Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Extracting file lines assigning to a vector

Playing around with Rust and getting stuck on a basic assignment error.

Here’s my code:

fn parse_file_lines(filename: &str) -> Vec<&str> {
    let mut file_lines: Vec<&str> = Vec::new();
    if let Ok(lines) = read_lines(filename) {
        lines.for_each(|line| match line {
            Ok(l) => {
                // perform some logic here...
                file_lines.push(&l)
            }
            Err(_) => todo!(),
        })
    }

    return file_lines;
}

fn read_lines(filename: &str) -> io::Result<io::Lines<io::BufReader<File>>> {
    let file = File::open(filename)?;
    Ok(io::BufReader::new(file).lines())
}

Which is returning this compiler error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

error[E0597]: `l` does not live long enough
  --> src/main.rs:17:38
   |
14 |     let mut file_lines: Vec<&str> = Vec::new();
   |         -------------- lifetime `'1` appears in the type of `file_lines`
...
17 |             Ok(l) => file_lines.push(&l),
   |                      ----------------^^-
   |                      |               | |
   |                      |               | `l` dropped here while still borrowed
   |                      |               borrowed value does not live long enough
   |                      argument requires that `l` is borrowed for `'1`

I know my scopes are off somehow but can’t figure out why – it’s been a long day 🥹

Thanks!

>Solution :

std::io::Lines yields owned Strings, not borrowed &strs, and thus you cannot drop them and store a reference to them in a Vec.

Instead, store the owned Strings in the Vec:

fn parse_file_lines(filename: &str) -> Vec<String> {
    let mut file_lines: Vec<String> = Vec::new();
    if let Ok(lines) = read_lines(filename) {
        lines.for_each(|line| match line {
            Ok(l) => {
                // perform some logic here...
                file_lines.push(l)
            }
            Err(_) => todo!(),
        })
    }

    return file_lines;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading