首页 文章

如何创建具有特定生命周期的可变String? [重复]

提问于
浏览
2

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

我有一个阅读文件内容的功能 . 我需要通过引用从这个函数返回内容,我只是无法弄清楚如何在函数内创建具有一定生命周期的可变 String .

fn main() {
    let filename = String::new();
    let content: &String = read_file_content(&filename);
    println!("{:?}", content);
}

fn read_file_content<'a>(_filename: &'a String) -> &'a String {
    let mut content: &'a String = &String::new();

    //....read file content.....
    //File::open(filename).unwrap().read_to_string(&mut content).unwrap();

    return &content;
}

输出:

error[E0597]: borrowed value does not live long enough
  --> src/main.rs:8:36
   |
8  |     let mut content: &'a String = &String::new();
   |                                    ^^^^^^^^^^^^^ does not live long enough
...
14 | }
   | - temporary value only lives until here
   |
note: borrowed value must be valid for the lifetime 'a as defined on the function body at 7:1...
  --> src/main.rs:7:1
   |
7  | / fn read_file_content<'a>(_filename: &'a String) -> &'a String {
8  | |     let mut content: &'a String = &String::new();
9  | |
10 | |     //....read file content.....
...  |
13 | |     return &content;
14 | | }
   | |_^

Playground

1 回答

  • 5

    在我看来,您的代码中存在逻辑错误 . 您试图将文件内容的生命周期限制为文件名的生命周期,因此绝对没有理由 . 您的代码无法编译的原因是该行

    let mut content: &'a String = &String::new();
    

    有效地阅读

    let mut anonymous = String::new();
    let mut content: &'a String = &anonymous;
    

    我称之为 anonymous 的变量的寿命不长于函数 . 使它比函数更长寿的唯一方法是使用引用计数类型来包装字符串,这样当它超出范围时它不会被删除,但只有当引用计数减少到0时才会被删除 .


    我建议你简单地抛弃引用语义并使用值代替 . 在 main 以外的函数中调用 unwrap 也不是好习惯,所以我通过一些错误处理补充了你的代码 .

    如果您担心性能,返回值将被移动(不复制),因此在此简化中绝对没有性能损失 .

    use std::io::prelude::*;
    use std::fs::File;
    
    fn main() {
        let filename = String::new();
        let content = read_file_content(&filename).unwrap();
        println!("{:?}", content);
    }
    
    fn read_file_content(filename: &str) -> std::io::Result<String> {
        //....read file content.....
        let mut content = String::new();
        let mut file = File::open(filename)?;
        file.read_to_string(&mut content)?;
        Ok(content)
    }
    

    Playground

相关问题