Understaning the notation of lifetime

I am trying to understand what lifetime notation mean in different situation.
For example:

trait SelfLifeTime {
    type T<'a> where Self: 'a;

What does the Self: 'a means? Does it mean that any type that is implementing SelfLifeTime has a lifetime 'a?
And is T<'a> accually a type? Liking T<i32> and T<f32> is two different types. T<'a> and T<'b>, when 'b: 'a, are they the same type? When 'a and 'b have no overlapping, are they the same type?


trait ForLifeTime {}

impl<T, N> ForLifeTime for T
    T: for<'l> SelfLifeTime<T<'l> = N>,

Does that mean: For any type, which implemented on SelfLifetime<T<'l> = N>, no mattter how long the 'l is, will implement on trait ForLifeTime?
Or does that mean: A type, which implemented on SelfLifeTime<T<'l> = N>, the 'l is something that can be super lifetime of any lifetime, (like in Julia, Any can be super type of any type) ?

No. It means that Self must be able to live at least as long as 'a. It may live longer. For example, Box<u32> is 'static, which means that it may potentially live until the end of program, but it may also be dropped earlier.

In other words, "Self has a lifetime 'a" is not incorrect, but remember that it's just one of the possible lifetimes, and not the exact region where Self is live. It's more like "Self can have lifetime 'a".

Yes, but it's not T, it's SelfLifeTime::T.

No, but the lifetimes can not be explicitly declared, and the borrow checker has quite a lot of freedom in deducing the exact lifetime. This means that while different lifetimes give different types (and in fact it's sometimes used in clever APIs), it may be far from obvious what the exact lifetimes are and whether they can be equal.

Your statement has grammatical issues so I can't vet its details, but the general gist is correct. for<'l> means any possible lifetime, including 'static.


T: 'a in a trait bound means that the type is valid for creating references of type &'a T. This in turn implies that instances (values) of type T don't have any references of which the lifetime is shorter than 'a. It does not mean that values of type T necessarily live as long as 'a.

Yes, if a generic "type" T has a lifetime parameter like T<'a>, then it's not actually a type, it's a type constructor, and only T<'a> is a concrete type (for some concrete 'a). Just like Vec is not in itself a type, only Vec<i32> or Vec<String> etc. are.

No, they are not. Just like Vec<String> and Vec<i32> aren't the same type. If they were the same type, then there would be no point in having a lifetime parameter in the first place.

The for<'l> … syntax is simple universal quantification. T: for<'l> bound<'l> means "for all lifetimes 'l, T satisfies the following bounds that themselves depend on 'l".


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.