面试题答案
一键面试use std::fs::File;
use std::io::{BufRead, BufReader};
fn main() {
let file_path = "your_file_path.txt";
let target_word = "hello";
let mut count = 0;
if let Ok(file) = File::open(file_path) {
let reader = BufReader::new(file);
for line in reader.lines() {
if let Ok(text) = line {
if text.contains(target_word) {
count += 1;
}
}
}
}
println!("The number of lines containing '{}' is: {}", target_word, count);
}