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);
}

The fs::read_to_string function reads the entire file into a String in one call, which is convenient for smaller files like config files or templates. For large files, you’d want to use BufReader to read line by line and avoid loading everything into memory at once.

The .expect() call will panic with your message if the file can’t be read. In production code, you’d typically use match or the ? operator to handle the error gracefully instead of crashing. Rust’s ownership model means the contents string is automatically freed when it goes out of scope.