Am I missing something when it comes to lifetimes?

Viewed 209

I just started learning Rust, coming from a Java/JavaScript background, so bear with me because I am obviously missing something in my understanding of lifetimes.

fn main() {
    struct Appearance<'a> {
        identity:       &'a u64, 
        role:           &'a str
    };
    impl<'a> PartialEq for Appearance<'a> {
        fn eq(&self, other: &Appearance) -> bool {
            self.identity == other.identity && self.role == other.role
        }
    };
    let thing = 42u64;
    let hair_color = "hair color";
    let appearance = Appearance { 
        identity: &thing, 
        role: &hair_color 
    };
    let another_thing = 43u64;    
    let other_appearance = Appearance { 
        identity: &another_thing, 
        role: &hair_color 
    };
    println!("{}", appearance == other_appearance);
}

This is giving me a compilation error as the compiler reaches the other_appearance, telling me that another_thing does not live long enough. However, if I leave out the creation of other_appearance the program compiles and runs fine. Why am I getting this error?

2 Answers
Related