I'm currently reading a file and need to compare the first line against a hardcoded value. I'm doing this:
let mut file = BufferedReader::new(File::open(path));
let mut first: bool = true;
for a_line in file.lines() {
let line = a_line.unwrap();
if first && line.as_slice() != "[tag]" {
println!("Returning None");
return None;
}
first = false;
}
I know first
is true and println!("{}", line)
shows it to be "[tag]"
but the comparison always seems to return false. What am I missing?
There is probably a newline character in your line string. You can use the trim
method and its variations on the string before taking a slice of it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.