当我编写这样的函数时,
fn must_get_env<'a>(key: &'a str) -> &'a str {
match std::env::var(key) {
Ok(val) => {
return &val;
}
Err(_) => panic!("Missing required environment variable: {}", key),
}
}
我收到此错误:
error[E0597]: `val` does not live long enough
--> src/main.rs:19:21
|
19 | return &val;
| ^^^ borrowed value does not live long enough
...
22 | }
| - borrowed value only lives until here
我以为我对锈的寿命有了很好的了解,但显然没有。除了返回String
之外,我还有什么选择?