I'm iterating over the lines of a .txt file. Every 3rd line I'm doing some calculation, every 1st or 2nd line, I save a reference to the line.
// Signature of fn used below
fn common_char(l1: &str, l2: &str, l3: &str) -> char
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() != 2 {
panic!("{} expects a pathname", args[0])
}
let mut cum = 0u64;
let mut n = 0;
if let Ok(lines) = read_lines(&args[1]) {
let mut l1: &str = "";
let mut l2: &str = "";
for line in lines {
if let Ok(l) = line {
n += 1;
let rest = n % 3;
if rest == 0 {
let common = common_char(l1, l2, l.trim());
cum += item_to_score(common);
} else if rest == 1 {
l1 = l.trim();
} else {
l2 = l.trim();
}
}
}
}
println!("The score is {}.", cum)
}
The compiler complains that
error[E0597]: `l` does not live long enough
--> src/main.rs:48:26
|
45 | let common = common_char(l1, l2, l.trim());
| -- borrow later used here
...
48 | l1 = l.trim();
| ^^^^^^^^ borrowed value does not live long enough
...
52 | }
| - `l` dropped here while still borrowed
I don't really get what's happening here, the branches can't run during the same iteration of the loop and every loop iteration gets a new line.