I'm trying to use a HashMap<String, &Trait>
but I have an error message I don't understand. Here's the code (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);
}
Here's the error I'm getting:
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
What's happening here? Is there a workaround?