Value moved here, in previous iteration of loop?

Hi, any help?, a solution to clarify ?. Thanks

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
}
1 Like

Aaaaa!. Thanks.

  1. Hi, this method does not exist -> read_to_string
  2. alernate i use "append_to_string" and error say -> function is private

:frowning:

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.

yes, my rust version is 1.31
:frowning:

Gosh, that's an old compiler :grinning:

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
}

Jejeje, in the jungle, there is no internet

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.