#[derive(Debug)]
struct Earth {
location: String,
}
#[derive(Debug)]
struct Dinosaur<'a> {
location: &'a Earth,
name: String,
}
fn main() {
let new_york = Earth {
location: "New York, NY".to_string(),
};
let t_rex = Dinosaur {
location: &new_york,
name: "T Rex".to_string(),
};
println!("{:?}", t_rex);
}
In the above example, you can see a lifetime annotation of 'a. When our struct borrows an instance of Earth, it needs the added lifetime marker. This helps the compiler to know that a Dinosaur should not outlive Earth, which it holds a reference to.
Whenever a struct holds a reference to another struct, isn't it obvious lifetime of struct that holds
must be <= lifetime of struct that is hold
?
I tried taking off the lifetime annotation and I got an error.
Why can't the compiler simply annotate for me the correct lifetime?