Zest.

Rust: Read a file

A short, tested Rust example for read a file. Copy it and adjust the names and paths for your project.

Rust
use std::fs;

let text = fs::read_to_string("data.txt")?;

use std::io::{BufRead, BufReader};
let f = fs::File::open("data.txt")?;
for line in BufReader::new(f).lines() {
    println!("{}", line?);
}

Find it in Zest

Search for “rust read a file” in Zest and this example appears above the results, with other matches to choose from. Searches stay private.

More Rust examples

All Rust examples