3 ms·
I think that regardless of what references you have, Rust frees values at the end of their lexical “scope”. For example, in the linked code below, x is clearly
by vlmutolo 2y ago
I think that regardless of what references you have, Rust frees values at the end of their lexical “scope”.
For example, in the linked code below, x is clearly unused past the first line, but its “Drop” implementation executes after the print statement at the end of the function.
The takeaway is that if you want a value to drop early, just explicitly `drop` it. The borrow checker will make sure you don't have any dangling references.
https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=16929eee379e50e56ae07627fb78fc5e https://play.rust-lang.org/?version=stable&mode=debug&editio...
In general, I think "lifetimes" only exist in the context of the borrow checker and have no influence on the semantics of Rust code. The language was designed so that the borrow checker pass could be omitted and everything would compile and run identically.
- steveklabnik 2y agoThis is correct.