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:
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;
}