My hypothesis is that Rust uses the term lifetime in a confusing way, which may lead people learning Rust to make wrong assumptions, and form an inaccurate mental model of lifetimes.
In computer science, outside of Rust, the term "lifetime" has a broader meaning of any time span from creation (allocation) of an object to its invalidation (deallocation). The term can be applied to garbage-collected reference types, or malloc
and free
, or C++ new
and delete
. Objects can freely move between scopes during their "lifetime" (in Rust borrowing prevents moves). GC languages automatically extend objects' "lifetimes" (Rust can't).
In Rust, it comes as a surprise that only references have a lifetime. Other types can be said to live for a certain amount time, but that is not their lifetime. It's unexpected that lifetimes are limited by the scopes they originated from, and don't fully match how long a borrowed object actually lives for.
Without understanding of finer borrowing details, it seems weirdly inconsistent that a generic T: 'static
bound on a function requires a T == &str
to live until the very end of the program, regardless of how long it's actually needed for in the function, but a T == String
satisfies the 'static
lifetime bound even when it doesn't live forever, and could be destroyed in the function or soon after the call.
&'a str
is a reference that has a lifetime, but Box<str>
is also a kind of a "reference" type, and it has its own "lifetime", but these are different kinds of references with different kinds of lifetimes!
I think it could be helpful to adjust Rust's terminology and migrate the term lifetime to something else, but I'm not sure what other term would be better. Any ideas?