我是 Rust 整个生命周期概念的新手。我正在尝试从 CSV 文件中读取一些数据并将它们放入HashMap
:
extern crate csv;
use std::collections::HashMap;
fn main() {
let files = vec!["file1.csv", "file2.csv", "file3.csv"];
let mut topics: HashMap<(&str, &str), &str> = HashMap::new();
for filename in files {
let mut rdr = csv::Reader::from_path(filename).unwrap();
for rec in rdr.records() {
let rr = rec.unwrap();
let value1 = rr.get(0).unwrap();
let value2 = rr.get(1).unwrap();
topics.insert((filename, value1), value2);
}
}
}
但是会出现以下错误:
error[E0597]: `rr` does not live long enough
--> src/main.rs:14:26
|
14 | let value1 = rr.get(0).unwrap();
| ^^ borrowed value does not live long enough
...
17 | }
| - `rr` dropped here while still borrowed
18 | }
19 | }
| - borrowed value needs to live until here
我认为插入HashMap
转移所有权,因此记录也可以在循环之外使用。我在这里做错了什么?