let vec_criterio = vec!["Title", "AKA", "Year"];
let title = String::new();
let aka = String::new();
let year = String::new();
// ? open file
let info = File::open(filename).unwrap();
let reader = BufReader::new(info);
for criterio in vec_criterio.into_iter() {
loop {
let mut found = false;
for (_index, line) in reader.lines().enumerate() {
let line = line.unwrap();
if found != false {
println!("Value: {}", line);
break;
} else if line.contains(criterio) {
found = true;
}
} // end for (_index, line)
} // end loop
} // end criterio
OUTPUT:
for (_index, line) in reader.lines().enumerate() {
| ^^^^^^ value moved here, in previous iteration of loop
Your BufReader gets consumed when you call reader.lines(). Instead of BufReader, read the contents of the file into a String. String also offers a lines method you can use, which does not consume the String, making it available beyond the first iteration:
use std::io::read_to_string;
use std::fs::File;
fn main() {
let vec_criterio = vec!["Title", "AKA", "Year"];
let filename = "foo.bar";
let info = File::open(filename).unwrap();
let reader: String = read_to_string(info).unwrap();
for criterio in vec_criterio.into_iter() {
loop {
let mut found = false;
for (_index, line) in reader.lines().enumerate() {
if found != false {
println!("Value: {}", line);
break;
} else if line.contains(criterio) {
found = true;
}
} // end for (_index, line)
} // end loop
} // end criterio
}
Read::read_to_string() most certainly does exist: Read in std::io - Rust. If you were trying to use std::io::read_to_string from jofas's code, then it would seem your Rust version is less than 1.65: read_to_string in std::io - Rust.
Try std::fs::read_to_string, which was added in 1.26. It is even more concise, because it opens the file as well:
use std::fs::read_to_string;
fn main() {
let vec_criterio = vec!["Title", "AKA", "Year"];
let filename = "foo.bar";
let reader: String = read_to_string(filename).unwrap();
for criterio in vec_criterio.into_iter() {
loop {
let mut found = false;
for (_index, line) in reader.lines().enumerate() {
if found != false {
println!("Value: {}", line);
break;
} else if line.contains(criterio) {
found = true;
}
} // end for (_index, line)
} // end loop
} // end criterio
}