No, lifetimes are not always the same for references to the same variable. There can be at least as many different lifetimes for references to the same variable as there are independent borrow expressions of that variable.
It can, but it doesn’t have to.
Lifetimes are not deduced; they are inferred. That means for any given piece of code, there may be several possible lifetimes that make it work. For each borrow expression, the compiler can pick any valid lifetime as long as it meets all the criteria.
Consider this example with a single lifetime:
let x = "Cryptid";
r1 = &x;
The lifetime of the borrow expression
&x is not constrained to the inner scope it is created in. The compiler is free to choose "the entire scope of the variable
x" as the lifetime of
&x. The above code compiles and does what you would expect. It’s the same with your example:
let a: u8 = 42;
let r1 = &a;
let mut r1t: InvariantLifetime<>;
r1t = get_lifetime(r1);
let r2 =&a;
let mut r2t = get_lifetime(r2);
r2t = r1t;
r1t = r2t;
The compiler is still free to choose "the entire scope of
a" as the lifetime of both borrow expressions, and since that makes the code compile, that’s what it will do.
Incidentally, although it doesn’t matter for this example, I don’t think
get_lifetime does what you want it to (make an “invariant copy” of the input lifetime). Instead, it materializes a new invariant lifetime derived from the input lifetime. Here’s the example I would have written that uses
&mut &'a T as the parameter to make sure the compiler can’t use the variance of
&'a T to decouple the lifetimes.