Please consider this example (playground):
struct Container<'short, 'long: 'short, T: 'long> {
short_prop: &'short u8,
long_prop: &'long T,
}
fn main() {
let value = 9;
let long = &value;
{
let short = &value;
let res = Container {
short_prop: long,
long_prop: short, // why is this not an error?
};
println!("{} {}", res.short_prop, res.long_prop);
}
}
In Container
I specify 'long: 'short
which I'm assuming means that 'long
should live at least as long as 'short
. In main
, the variable long
should live longer than short
because it has a bigger scope.
So Container
expects whatever goes into long_prop
to live at least as long as whatever goes into short_prop
. Yet the compiler is perfectly happy with me passing the longer-lived long
into short_prop
and the shorter-lived short
into long_prop
.
Is this because at the instantiation of Container
the compiler narrows the lifetime of long
to be equal to the lifetime of short
thus satisfying the 'long: 'short
constraint (since equality of lifetimes is fine)? Or is there another reason?