这个问题在这里已经有了答案:
Borrow checker doesn't realize that `clear` drops reference to local variable
(6 个回答)
去年关闭。
我正在处理一个巨大的 TSV(制表符分隔值)文件,并希望尽可能高效地执行此操作。为此,我想我会阻止分配一个新的 Vec
通过在循环之前预先分配每一行:
let mut line = String::new();
let mut fields = Vec::with_capacity(headers.len());
while reader.read_line(&mut line)? > 0 {
fields.extend(line.split('\t'));
// do something with fields
fields.clear();
}
自然地,借用检查器不会被逗乐,因为我们正在覆盖
line
而 fields
可能仍然有引用:error[E0502]: cannot borrow `line` as mutable because it is also borrowed as immutable
--> src/main.rs:66:28
|
66 | while reader.read_line(&mut line)? > 0 {
| ^^^^^^^^^ mutable borrow occurs here
67 | fields.extend(line.split('\t'));
| ------ ---- immutable borrow occurs here
| |
| immutable borrow later used here
( Playground )
这实际上不是问题,因为
fields.clear();
删除所有引用,因此在循环开始时 read_line(&mut line)
被称为,fields
实际上并没有从 line
借任何东西.但是我如何通知借阅检查员这一点?
最佳答案
您的问题与 this post 中描述的问题相似.
除了那里的答案(生命周期变换、引用单元格),根据您注释掉的复杂操作,您可能不需要存储对 line
的引用。根本。例如,考虑对您的游乐场代码进行以下修改:
use std::io::BufRead;
fn main() -> Result<(), std::io::Error> {
let headers = vec![1,2,3,4];
let mut reader = std::io::BufReader::new(std::fs::File::open("foo.txt")?);
let mut fields = Vec::with_capacity(headers.len());
loop {
let mut line = String::new();
if reader.read_line(&mut line)? == 0 {
break;
}
fields.push(0);
fields.extend(line.match_indices('\t').map(|x| x.0 + 1));
// do something with fields
// each element of fields starts a field; you can use the next
// element of fields to find the end of the field.
// (make sure to account for the \t, and the last field having no
// 'next' element in fields.
fields.clear();
}
Ok(())
}
关于rust - 如何告诉借用检查器已清除的 Vec 不包含借用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61339014/