"Geliehener Wert lebt nicht lange genug" scheint die falsche Sache zu beschuldigen
Ich zähle, wie oft ein Wort in Macbeth vorkommt:
use std::io::{BufRead, BufReader};
use std::fs::File;
use std::collections::HashMap;
fn main() {
let f = File::open("macbeth.txt").unwrap();
let reader = BufReader::new(f);
let mut counts = HashMap::new();
for l in reader.lines() {
for w in l.unwrap().split_whitespace() {
let count = counts.entry(w).or_insert(0);
*count += 1;
}
}
println!("{:?}", counts);
}
Vertraue auf diesen Punkt und sage:
error[E0597]: borrowed value does not live long enough
--> src/main.rs:14:9
|
11 | for w in l.unwrap().split_whitespace() {
| ---------- temporary value created here
...
14 | }
| ^ temporary value dropped here while still borrowed
...
18 | }
| - temporary value needs to live until here
|
= note: consider using a `let` binding to increase its lifetime
Das eigentliche Problem ist, dassw
ist eine Referenz und ändert sie inw.to_string()
löst es. Ich verstehe nicht, warum der Rust-Compiler die Schuld auf @ richtl
, wenn das Problem @ iw
. Wie soll ich darauf schließen, dassw
ist das Problem hier?