缓冲读卡器的线路寿命不足可防止分线

时间:2018-03-21 20:39:41

标签: rust lifetime borrowing

我正试图弄清楚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的生命周期或借用属性?

1 个答案:

答案 0 :(得分:1)

编译时原始代码给出的错误是:

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

您正在创建包含对Vec切片的引用的str;切片不拥有从中切出的数据,它们实际上是指向必须由另一个变量拥有的数据的指针。因此,切片的变量必须比切片寿命长。

在用于初始化Vec的表达式中,您创建了一个String,其中包含您正在处理的文本行。该字符串的作用域是变量ln是初始化表达式 - 一旦离开该范围,它就会被删除。

然后你split字符串,它返回一个字符串切片的迭代器,每个子字符串一个。但请记住,迭代器是返回切片,它是指向String ln中子串的指针。这些切片不允许超过ln本身。

希望您现在可以看到问题。退出初始化表达式后,ln将被删除,但Vec仍会包含str个切片。他们指的是什么?

修复非常简单。为什么在该块中声明ln?事实上为什么有一个块呢?这有效:

for line in f.lines() {
    let ln: String = line.unwrap();
    let split: Vec<&str> = ln.split(' ').collect();
    // Now do something with split
}