📚Cheatsheets

Cheatsheet collection for go, rust, python, shell and javascript.

Read file to string

Read file to string in Rust.

Read to string

let file = "data.txt"
let data = std::fs::read_to_string(file).expect(&format!("Filed reading file {}", file));

Read file line by line

let file = File::open(path);
if let Ok(file) = file {
    let mut reader = BufReader::new(file);
    loop {
        let mut line = String::new();
        match reader.read_line(&mut line) {
            Ok(_) => {
                if line.is_empty() {
                    break;
                }
                // do anything with line
            }
            _ => break,
        };
    }
}