我想从"input.txt"读取字符串,只留下那些#在行的开头没有(注释)符号的字符串.我写了这段代码:
use std::io::{BufRead, BufReader};
use std::fs::File;
fn main() {
let file = BufReader::new(File::open("input.txt").unwrap());
let lines: Vec<String> = file.lines().map(|x| x.unwrap()).collect();
let mut iter = lines.iter().filter(|&x| x.chars().next() != "#".chars().next());
println!("{}", iter.next().unwrap());
}
Run Code Online (Sandbox Code Playgroud)
但这一行
|&x| x.chars().next() != "#".chars().next()
Run Code Online (Sandbox Code Playgroud)
闻起来对我不好,因为它看起来像这样|x| x[0] == "#",我无法检查字符串中的第二个字符.
那我怎么能重构这段代码呢?