-
StackOverflow 文档
-
Rust 教程
-
文件 IO
-
逐行读取文件
use std::fs::File;
use std::io::{BufRead, BufReader};
fn main() {
let filename = "src/main.rs";
// Open the file in read-only mode (ignoring errors).
let file = File::open(filename).unwrap();
let reader = BufReader::new(file);
// Read the file line by line using the lines() iterator from std::io::BufRead.
for (index, line) in reader.lines().enumerate() {
let line = line.unwrap(); // Ignore errors.
// Show the line and its number.
println!("{}. {}", index + 1, line);
}
}