Comment on Common Rust Lifetime Misconceptions (2020)Comments−almost_usual2yOnce I discovered ‘static was the subtype of all lifetimes rather than the super things began clicking for me.−cwzwarich2y`static is actually the superlifetime of all lifetimes, but & is contravariant in its lifetime parameter (and covariant in its type parameter).−almost_usual2yMind expanding on this? The nomicon describes &’a T where both ‘a’ and ‘T’ are covariant. I thought I had a clear picture but now I’m confused.https://doc.rust-lang.org/nomicon/subtyping.html#variance−cwzwarich2yHere is the original GitHub issue on the question:https://github.com/rust-lang/rust/issues/15699And an RFC by some people that felt frustrated by this arguably implementation-centric view that kind of lost steam:https://github.com/rust-lang/rfcs/issues/391Intuitively, the bottom lifetime should the one that is uninhabited, which would be a lifetime with no extent rather than 'static.−almost_usual2yInteresting, thanks for sharing!−Georgelemental2y`rustc` used to use different terminology than the nomicom, but this is no longer the case: https://github.com/rust-lang/rust/pull/107339−Georgelemental2yThis is not true anymore: https://github.com/rust-lang/rust/pull/107339−andyferris2yCould you elaborate? I find this unintuitive.−csjh2ySince static lifetimes last for the entirety of the program, it can sub in for any other lifetime (as it is guaranteed to exist until the end of the other lifetime)−raincole2yI am not familiar with the formal type theory, but this one is intuitive if you view it this way:1. When A is a subtype of B, it means A can be used as B (a Teacher can be used by any function that accepts a Human).2. static lifetime lives longer any other lifetimes, so it can be used as other lifetimes.
Comments
Once I discovered ‘static was the subtype of all lifetimes rather than the super things began clicking for me.
`static is actually the superlifetime of all lifetimes, but & is contravariant in its lifetime parameter (and covariant in its type parameter).
Mind expanding on this? The nomicon describes &’a T where both ‘a’ and ‘T’ are covariant. I thought I had a clear picture but now I’m confused.
https://doc.rust-lang.org/nomicon/subtyping.html#variance
Here is the original GitHub issue on the question:
https://github.com/rust-lang/rust/issues/15699
And an RFC by some people that felt frustrated by this arguably implementation-centric view that kind of lost steam:
https://github.com/rust-lang/rfcs/issues/391
Intuitively, the bottom lifetime should the one that is uninhabited, which would be a lifetime with no extent rather than 'static.
Interesting, thanks for sharing!
`rustc` used to use different terminology than the nomicom, but this is no longer the case: https://github.com/rust-lang/rust/pull/107339
This is not true anymore: https://github.com/rust-lang/rust/pull/107339
Could you elaborate? I find this unintuitive.
Since static lifetimes last for the entirety of the program, it can sub in for any other lifetime (as it is guaranteed to exist until the end of the other lifetime)
I am not familiar with the formal type theory, but this one is intuitive if you view it this way:
1. When A is a subtype of B, it means A can be used as B (a Teacher can be used by any function that accepts a Human).
2. static lifetime lives longer any other lifetimes, so it can be used as other lifetimes.