借入的价值没有足够长的循环寿命 [英] borrowed value does not live long enough in loop

查看:55
本文介绍了借入的价值没有足够长的循环寿命的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析文件并从函数中返回 Vec< Vec< str>> .但是在推送到向量时,我在文件读取循环中遇到借入值错误.

I'm trying to parse a file and return Vec<Vec<&str>> from a function. But I'm getting borrowed value error inside the file read loop while pushing to the vector.

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

我认为这与参考资料和借阅有关,但是我仍然很难弄清这一点.

I think it's about the references and borrowing but still I'm having hard time to figure this out.

推荐答案

此问题类似于

This question is similar to Return local String as a slice (&str). And the easiest solution is the same as in that question - use String, not &str. These questions are different as that answer specifically talks about returning from a function, and you have no function listed.

要解决生命周期使代码失败的原因,请尝试一个更简单的示例

To address why lifetimes make your code fail, try a simpler example

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()中删除旧的 str s使堆栈变小.每次循环时都收集::< Vec< str>>().

The reason this doesn't work is exactly what the compiler says. chars is created inside the block, so it gets a lifetime associated with that block, and when your program exits that block, chars might not exist anymore. Anything that referred to chars might have a dangling pointer. Rust avoids dangling pointers by making this illegal. In my example it seems silly that Rust doesn't allow this, but in yours it makes sense - Rust can keep the stack small by deleting the old strs from v.split_whitespace().collect::<Vec<&str>>() every time through the loop.

这篇关于借入的价值没有足够长的循环寿命的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆