Hi! Thanks for your demonstration of two situation of same lifetime.
Recently I found a situation in which two references of different variables share same lifetime and that makes me confused.
let mut x2: u8 = 1;
{
let mut x = 42;
let mut y = &x;
let mut yt: InvariantLifetime;
yt = get_lifetime(&mut y);
let mut y2 = &x2;
let mut y2t: InvariantLifetime;
y2t = get_lifetime(&mut y2);
y2t = yt;
}
These code could be compiled, which imply that yt
and y2t
carry same lifetime.
In above code, some type and function is implemented as(ref):
pub struct Invariant<T>(PhantomData<*mut T>);
pub struct InvariantLifetime<'id>(Invariant<&'id ()>);
fn get_lifetime<'a, T>(_: &mut &'a T) -> InvariantLifetime<'a> { InvariantLifetime::new() }
Does these code shows that two references(yt
,y2t
) have save timelife?