- enumerate
- lines
Rust - read file line-by-line with row number (enumerate)
examples/files/read-line-by-line-enumerate/src/main.rs
use std::fs::File; use std::io::{BufRead, BufReader}; fn main() { let filename = "rust.json"; match File::open(filename) { Ok(file) => { let reader = BufReader::new(file); for (index, line) in reader.lines().enumerate() { let line = line.unwrap(); println!("{}. {}", index + 1, line); } } Err(error) => { println!("Error opening file {}: {}", filename, error); } } }