Rust 参数要求为 `'static` 借用 `variable (String)`

问题描述 投票:0回答:0

我的代码:

let output_utf8: String = String::from_utf8(output).unwrap();
let lines = output_utf8.split("\n").collect::<Vec<&str>>();

let mut sensors: Vec<Sensor> = Vec::new();
for ln in lines {
    let mut sensor_fields = ln.split(",");
    sensors.push(
        //...
    );
}

错误:

error[E0597]: `output_utf8` does not live long enough
  --> src/main.rs:25:17
   |
25 |     let lines = output_utf8.split("\n").collect::<Vec<&str>>();
   |                 ^^^^^^^^^^^^^^^^^^^^^^^ borrowed value does not live long enough

argument requires that `output_utf8` is borrowed for `'static`

如何借

output_utf8
换取
'static
? 我试过
.clone()
.to_owned()
.into()
,都没有用。 为什么我需要
String
的静态生命周期?

rust lifetime borrow-checker
© www.soinside.com 2019 - 2024. All rights reserved.