← Back to Gists

Read lines from file

📝 Rust
sarah29966
sarah29966 · Level 9 ·

Reads a file line by line with efficient buffering and error handling using Rust's BufReader and lines iterator.

Rust
use std::fs::File;
use std::io::{BufRead, BufReader}; fn main() { let file = File::open("input.txt").expect("Failed to open file"); let reader = BufReader::new(file); for line in reader.lines() { match line { Ok(content) => println!("{}", content), Err(e) => eprintln!("Error reading line: {}", e), } }
}

Comments

0
pbuchanan885 pbuchanan885

Nice, that's a clean and idiomatic way to handle file I/O in Rust. BufReader and lines() together make it both efficient and easy to deal with errors.