Read file contents to string safely I've just started to teach myself Rust. I've written the following code to read a text file and store the contents in a String. Is this the typical way of doing this? Can anyone suggest any improvements to this?

use std::fs::File;
use std::io::Read;
use std::io;

fn main() {

    let file_name = "test.txt";

    let mut file_contents = String::new();

    match get_file_contents(file_name, &amp;mut file_contents) {
        Ok(()) =&gt; (),
        Err(err) =&gt; panic!("{}", err)
    };

    println!("{}", file_contents);

}

fn get_file_contents(name: &amp;str, mut contents: &amp;mut String) -&gt; Result&lt;(), io::Error&gt; {

    let mut f = try!(File::open(name));

    try!(f.read_to_string(&amp;mut contents));

    Ok(())

}
You could return the string directly:

fn main() {
    let file_name = "test.txt";
    let file_contents = match get_file_contents(file_name) {
        Ok(s) =&gt; s,
        Err(err) =&gt; panic!("{}", err)
    };

    println!("{}", file_contents);
}

fn get_file_contents(name: &amp;str) -&gt; Result&lt;String, io::Error&gt; {
    let mut f = try!(File::open(name));
    let mut contents = String::new();

    try!(f.read_to_string(&amp;mut contents));

    Ok(contents)
}


and unwrap if you don’t intend to handle the error usefully:

let file_contents = get_file_contents(file_name).unwrap();