我正在绞尽脑汁想弄清楚 Rust 的借用/生命周期/所有权属性。即,当使用缓冲读取器并尝试拆分一行时。代码
use std::fs::File;
use std::io::{BufRead, BufReader};
fn main() {
let f = File::open("foo.txt").expect("file not found");
let f = BufReader::new(f);
for line in f.lines() {
let split: Vec<&str> = {
let ln: String = line.unwrap();
ln.split(' ').collect()
};
}
}
或任何变体(有或没有指定变量类型,徒劳地尝试使其可变等)导致:
'ln' does not live long enough; borrowed value must only be valid for the static lifetime...
还试图假装延长生命周期并通过切片从线路中获取一些数据
let nm = line;
name = &line[..];
甚至只是尝试在未修改的行变量上运行 split()
会导致:
cannot index into a value of type 'std::result::Result<std::string::String, std::io::Error>'
"borrowed value does not live long enough" seems to blame the wrong thing建议生命周期持续足够长的时间以将每个单词放入其自己的字符串中,但修改我在 the Playground 上的原始代码包含嵌套的 for 循环仍然会导致
error[E0597]: borrowed value does not live long enough
--> src/main.rs:11:18
|
11 | for w in line.unwrap().split_whitespace() {
| ^^^^^^^^^^^^^ temporary value does not live long enough
...
14 | }
| - temporary value dropped here while still borrowed
15 | }
| - temporary value needs to live until here
|
= note: consider using a `let` binding to increase its lifetime
引用line.unwrap()
最后,我对 Rust 的生命周期或借用属性有什么误解?
最佳答案
您的原始代码在编译时给出的错误是:
error[E0597]: `ln` does not live long enough
--> src/main.rs:11:13
|
11 | ln.split(' ').collect()
| ^^ borrowed value does not live long enough
12 | };
| - `ln` dropped here while still borrowed
13 | }
| - borrowed value needs to live until here
error: aborting due to previous error
根据@shepmasters 的评论,在发布问题时提供完整的错误是个好主意。
无论如何,它突出了问题:
let split: Vec<&str> = {
let ln: String = line.unwrap();
ln.split(' ').collect()
};
您正在创建一个包含对 str
切片的引用的 Vec
;切片不拥有从中切片的数据,它们实际上是指向必须由另一个变量拥有的数据的指针。因此,从中切片的变量必须比切片存在得更久。
在用于初始化 Vec
的表达式中,您创建了一个包含您正在处理的文本行的 String
。此字符串的范围是变量 ln
是初始化表达式 - 一旦您离开该范围,它将被删除。
然后您拆分
字符串,它返回一个迭代器到字符串切片,每个子字符串一个。但请记住,迭代器正在返回切片,这些切片是指向 String
ln
中的子字符串的指针。这些切片不允许超过 ln
本身。
希望您现在可以看到问题所在。一旦退出初始化表达式,ln
就会被丢弃,但 Vec
仍将包含 str
切片。他们指的是什么?
修复非常简单。为什么要在该 block 内声明 ln
?事实上,为什么在那里有一个街区?这有效:
for line in f.lines() {
let ln: String = line.unwrap();
let split: Vec<&str> = ln.split(' ').collect();
// Now do something with split
}
关于rust - 缓冲读取器缺少行的生命周期可防止拆分行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49416082/