Why is this safe, or allowed by the compiler?
I have been experimenting with different ways to avoid cloning hash map keys. This option, interestingly, is permitted by the compiler. But I don't understand why. The name string can be removed from the list of names, does the age field keep it alive?
use std::collections::HashMap;
#[derive(Debug)]
struct Sample<'a> {
names: Vec<String>,
age: HashMap<&'a str,u8>,
}
fn main() {
let mut s = Sample{
names: Vec::new(),
age: HashMap::new(),
};
s.names.push("john".to_string());
s.age.insert(s.names.last().unwrap().as_ref(), 10);
println!("{:?}", s);
}