4 ms·
This... isn't how lifetimes work. As I said above, there's no lifetime annotations you can use to ensure that y lives as long(aka is in the same scope) as x in
by dbaupp 12y ago
This... isn't how lifetimes work.
As I said above, there's no lifetime annotations you can use to ensure that y lives as long(aka is in the same scope) as x in the following
let x = 1;
{
let y = 2;
}
This is also true if you do dynamic allocations: there's fundamentally no way to "promote" variables to a higher scopes just by using lifetime annotations.
Lifetimes are a purely passive thing, designed to allow the compiler to check the programmer's use of references, not to allow the compiler to massage things around to ensure that whatever use work.
That is, if a Rust program compiles, then it will also compile (modulo needing extra `unsafe` annotation) and, importantly, do the same thing, if all the references are replaced with raw pointers, which don't have lifetimes to describe their relationships.
- Animats 12y agoYou're right. I thought you could use lifetime annotation to force something to be allocated in an outer scope. But Rust doesn't allow that.