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

ole*_*ole 2 rust

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

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;
}
Run Code Online (Sandbox Code Playgroud)

输出:

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 | | }
   | |_^
Run Code Online (Sandbox Code Playgroud)

操场

Hen*_*nke 5

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

let mut content: &'a String = &String::new();
Run Code Online (Sandbox Code Playgroud)

有效地阅读

let mut anonymous = String::new();
let mut content: &'a String = &anonymous;
Run Code Online (Sandbox Code Playgroud)

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


我建议你简单地抛弃引用语义并使用值代替.另外调用unwrap一个main非常好的函数,所以我通过一些错误处理补充了你的代码.

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

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)
}
Run Code Online (Sandbox Code Playgroud)

操场