Read a file line by line
Rust
use std::fs::File;
use std::io::{self, BufRead, BufReader};
use std::path::Path;
fn read_lines<P>(path: P) -> io::Result<Vec<String>>
where
P: AsRef<Path>,
{
let file = File::open(path)?;
let reader = BufReader::new(file);
reader.lines().collect()
}
fn main() -> io::Result<()> {
for (n, line) in read_lines("input.txt")?.iter().enumerate() {
println!("{:>4}: {}", n + 1, line);
}
Ok(())
}