rust - 如何告诉借用检查器已清除的 Vec 不包含借用?

标签 rust borrow-checker

这个问题在这里已经有了答案:





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();
}

自然地,借用检查器不会被逗乐,因为我们正在覆盖 linefields可能仍然有引用:
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/

相关文章:

rust - 如何从具有任意整数和分数分割的原始整数类型中解析补码定点数?

rust - 返回 Future 类型时无法推断类型 F

image - 来自 SubImage<&DynamicImage> 的动态图像

rust - 为什么在函数内部改变传递的选项不会传播到外部的选项?

error-handling - 为什么 Rust 编译器不使用 From 特性将库错误转换为我的错误?

rust - 为什么编译器告诉我考虑使用 `let` 绑定(bind)?

rust - 是否可以在 stdin.lines() 中间执行另一次读取?

javascript - 难以在 JavaScript 中实现简化的借用检查器

rust - 如何使 `unwrap_or` 返回一个由零组成的向量?

rust - Mutex 中的可变借用失败