How to Read a File in Rust


If you need to read a file in Rust, then you can use the fs package from the standard library:

use std::fs;

fn main() {
    let contents = fs::read_to_string(filename)
        .expect("Something went wrong reading the file");

    println!("With text:\n{}", contents);
}