【发布时间】:2015-08-24 00:00:09
【问题描述】:
我正在尝试使用HashMap<String, &Trait>,但我收到一条我不明白的错误消息。这是代码(playground):
use std::collections::HashMap;
trait Trait {}
struct Struct;
impl Trait for Struct {}
fn main() {
let mut map: HashMap<String, &Trait> = HashMap::new();
let s = Struct;
map.insert("key".to_string(), &s);
}
这是我得到的错误:
error[E0597]: `s` does not live long enough
--> src/main.rs:12:36
|
12 | map.insert("key".to_string(), &s);
| ^ borrowed value does not live long enough
13 | }
| - `s` dropped here while still borrowed
|
= note: values in a scope are dropped in the opposite order they are created
这里发生了什么?有解决办法吗?
【问题讨论】:
标签: rust borrow-checker