在循环中,借用值的生命周期不够长

7

我正在尝试解析一个文件,并从函数返回Vec<Vec<&str>>。但是在文件读取循环中将内容push到向量时,出现了借用值错误。

use std::io::{self, BufReader, prelude::*};
use std::fs::File;

fn read() -> Vec<Vec<&'static str>> {
 let file = File::open("~/test").expect("failed to read file");
 let reader = BufReader::new(file);
 let mut main_vector: Vec<Vec<&str>> = Vec::new();
    for line in reader.lines() {
        match line {
            Ok(v) => {
                let mut sub_vector: Vec<&str> = Vec::new();
                for element in v.split_whitespace().collect::<Vec<&str>>() {
                    sub_vector.push(element);
                }
                main_vector.push(sub_vector);
            },
            Err(e) => panic!("failed to parse: {:?}", e),
        }
    }
    //return main_vector;
}

以下是编译器错误:

error[E0597]: `v` does not live long enough
  --> src/main.rs:67:32
   |
67 |                 for element in v.split_whitespace().collect::<Vec<&str>>() {
   |                                ^ borrowed value does not live long enough
...
70 |                 main_vector.push(sub_vector);
   |                 -------------- borrow later used here
71 |             },
   |              - `v` dropped here while still borrowed

我认为这与引用和借用有关,但我仍然很难理解。
2个回答

5
这个问题类似于将本地字符串作为片段(&str)返回。最简单的解决方案与该问题相同-使用String而不是&str。 这些问题的不同之处在于该答案特别讨论从函数返回,并且您没有列出任何函数。 为了解释为什么生命周期使您的代码失败,请尝试一个更简单的示例。
fn main() {
    let mut v:Vec<&str> = Vec::new();
    {
        let chars = [b'x', b'y', b'z'];
        let s:&str = std::str::from_utf8(&chars).unwrap();
        v.push(&s);
     }
    println!("{:?}", v);
}

以及编译器输出


let s:&str = std::str::from_utf8(&chars).unwrap();
                                 ^^^^^^ borrowed value does not live long enough

这无法运作的原因正是编译器所说的。 `chars` 是在块内部创建的,因此它具有与该块相关联的生命周期,当程序退出该块时,`chars` 可能不再存在。任何引用 `chars` 的内容可能会有悬空指针。Rust 通过禁止这样做来避免出现悬空指针。在我的示例中,Rust 不允许这种情况似乎很傻,但在你的示例中,这是有意义的 - Rust 可以通过每次循环删除来自 `v.split_whitespace().collect::>()` 的旧 `str`,从而保持堆栈的小巧性。

使用 Vec<Vec<String>> 替代 Vec<Vec<&str>> 即可解决。谢谢! - orhun
感谢@orhun提供的关于String工作方式(在&str无法工作时)的信息。这也解决了我的问题。 - Jake Ireland

0

如果有更多的代码提供会更好,但是我试图复制它并得到了这个可用的片段:

type Error = i32;

struct Reader
{
    lines: Vec<Result<String, Error>>
}

impl Reader
{
    pub fn new() -> Self
    {
        Self{lines: vec![Ok("foo".into()), Ok("bar".into())]}
    }

    pub fn lines(&self) -> &[Result<String, Error>]
    {
        &self.lines
    }
}

fn main() {
    let reader = Reader::new();
    let mut main_vector: Vec<Vec<&str>> = Vec::new();
    for line in reader.lines() {
        match line {
            Ok(v) => {
                let mut sub_vector: Vec<&str> = Vec::new();
                for element in v.split_whitespace().collect::<Vec<&str>>() {
                    sub_vector.push(element);
                }
                main_vector.push(sub_vector);
            },
            Err(e) => panic!("failed to parse: {:?}", e),
        }
    }
}

您可以在 Rust Playground 上查看此处 https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=f2785fcad682b9dd1f5ed61c7e0308d8


我已经添加了代码的其他部分。这段代码片段运行良好,但我认为 BufReader(.lines())是问题所在。 - orhun

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接