Skip to main content
deleted 1 character in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Rust: Read file contents to string safely

Rust: 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, &mut file_contents) {
        Ok(()) => (),
        Err(err) => panic!("{}", err)
    };

    println!("{}", file_contents);

}

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

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

}

Rust: 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, &mut file_contents) {
        Ok(()) => (),
        Err(err) => panic!("{}", err)
    };

    println!("{}", file_contents);

}

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

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

}

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, &mut file_contents) {
        Ok(()) => (),
        Err(err) => panic!("{}", err)
    };

    println!("{}", file_contents);

}

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

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

}
Source Link
soarjay
  • 237
  • 1
  • 3

Rust: 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, &mut file_contents) {
        Ok(()) => (),
        Err(err) => panic!("{}", err)
    };

    println!("{}", file_contents);

}

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

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

}