Why is this safe, or allowed by the compiler?

50 points fengli

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);
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=86598a471c6d4771b4214645b4ae3697