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

时间:2018-01-11 22:43:51

标签: rust

我有一个读取文件内容的功能。我需要通过引用从这个函数返回内容,我无法弄清楚如何在函数内创建具有一定生命期的可变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 个答案:

答案 0 :(得分:5)

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

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

有效阅读

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

我称之为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)
}

Playground