言語処理100本ノックの4問目をRustで解いています。
use std::collections::HashMap;
pub fn problem04() {
let s = "Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can.";
let sw: Vec<&str> = s.split(" ").collect();
let mut map: HashMap<&str, usize> = HashMap::new();
for i in 0..sw.len() {
let word = sw[i].to_string();
match i {
1 | 5 | 6 | 7 | 8 | 9 | 15 | 16 | 19 => map.insert(&word[0..1], i),
_ => map.insert(&word[0..2], i),
};
};
println!("{:?}", map);
}
と書いて
error[E0597]: `word` does not live long enough
--> src/problem04.rs:12:30
|
12 | _ => map.insert(&word[0..2], i),
| ^^^^ borrowed value does not live long enough
13 | };
14 | };
| - `word` dropped here while still borrowed
15 | println!("{:?}", map);
| --- borrow later used here
error: aborting due to previous error
For more information about this error, try `rustc --explain E0597`.
error: Could not compile `nlp100`.
To learn more, run the command again with --verbose.
shell returned 101
とエラーが出ました。
所有権周りで躓いていると思うのですが、どなたか解決策をご教示いただけないでしょうか。
よろしくおねがいします。