我有一个从文件中读取的字符串。对于每一行,我想拆分字符串并将拆分字符串保存为内存作为向量:
use std::io::{BufReader, BufRead};
fn main() {
let s = r#"line1,line
line2,line2"#;
let reader = BufReader::new(s.as_bytes());
let mut out = Vec::new();
let iter = reader
.lines()
.map(|line| line.unwrap().to_string())
.collect::<Vec<_>>();
for x in iter {
let split = x.split(',').collect::<Vec<_>>();
out.push(split);
}
}
错误消息
error[E0597]: `x` does not live long enough
--> src/main.rs:18:6
|
16 | let split = x.split(',').collect::<Vec<_>>();
| - borrow occurs here
17 | out.push(split);
18 | }
| ^ `x` dropped here while still borrowed
19 | }
| - borrowed value needs to live until here