3
votes

I'm trying to parse a file and return Vec<Vec<&str>> from a function. But I'm getting borrowed value error inside the file read loop while pushing to the vector.

use std::io::{self, BufReader, prelude::*};
use std::fs::File;

fn read() -> Vec<Vec<&'static str>> {
 let file = File::open("~/test").expect("failed to read file");
 let reader = BufReader::new(file);
 let mut main_vector: Vec<Vec<&str>> = Vec::new();
    for line in reader.lines() {
        match line {
            Ok(v) => {
                let mut sub_vector: Vec<&str> = Vec::new();
                for element in v.split_whitespace().collect::<Vec<&str>>() {
                    sub_vector.push(element);
                }
                main_vector.push(sub_vector);
            },
            Err(e) => panic!("failed to parse: {:?}", e),
        }
    }
    //return main_vector;
}

Here's the compiler error:

error[E0597]: `v` does not live long enough
  --> src/main.rs:67:32
   |
67 |                 for element in v.split_whitespace().collect::<Vec<&str>>() {
   |                                ^ borrowed value does not live long enough
...
70 |                 main_vector.push(sub_vector);
   |                 -------------- borrow later used here
71 |             },
   |              - `v` dropped here while still borrowed

I think it's about the references and borrowing but still I'm having hard time to figure this out.

2

2 Answers

3
votes

This question is similar to Return local String as a slice (&str). And the easiest solution is the same as in that question - use String, not &str. These questions are different as that answer specifically talks about returning from a function, and you have no function listed.

To address why lifetimes make your code fail, try a simpler example

fn main() {
    let mut v:Vec<&str> = Vec::new();
    {
        let chars = [b'x', b'y', b'z'];
        let s:&str = std::str::from_utf8(&chars).unwrap();
        v.push(&s);
     }
    println!("{:?}", v);
}

and compiler output


let s:&str = std::str::from_utf8(&chars).unwrap();
                                 ^^^^^^ borrowed value does not live long enough

The reason this doesn't work is exactly what the compiler says. chars is created inside the block, so it gets a lifetime associated with that block, and when your program exits that block, chars might not exist anymore. Anything that referred to chars might have a dangling pointer. Rust avoids dangling pointers by making this illegal. In my example it seems silly that Rust doesn't allow this, but in yours it makes sense - Rust can keep the stack small by deleting the old strs from v.split_whitespace().collect::<Vec<&str>>() every time through the loop.

-1
votes

It would be better if more code was provided, but I tried to reproduce it and arrived at this working snippet:

type Error = i32;

struct Reader
{
    lines: Vec<Result<String, Error>>
}

impl Reader
{
    pub fn new() -> Self
    {
        Self{lines: vec![Ok("foo".into()), Ok("bar".into())]}
    }

    pub fn lines(&self) -> &[Result<String, Error>]
    {
        &self.lines
    }
}

fn main() {
    let reader = Reader::new();
    let mut main_vector: Vec<Vec<&str>> = Vec::new();
    for line in reader.lines() {
        match line {
            Ok(v) => {
                let mut sub_vector: Vec<&str> = Vec::new();
                for element in v.split_whitespace().collect::<Vec<&str>>() {
                    sub_vector.push(element);
                }
                main_vector.push(sub_vector);
            },
            Err(e) => panic!("failed to parse: {:?}", e),
        }
    }
}

You can check it on Rust Playground here https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=f2785fcad682b9dd1f5ed61c7e0308d8