Lifetime bound vs lifetime parameter

In the following code:

trait Tr {}

struct S<'t, T>(&'t T);

impl<'t, T> Tr for S<'t, T> {}

fn f<'a>(s: impl Tr + 'a) {

}

fn main() {
    let x = 1i32;
    let s = S(&x);
    f(s);
}

What's the relation between 't and 'a?

As far as I know, S<'t, ...> means that the lifetime of instance of S<'t, ...> (let's say it's 's) cannot outlive 't.

According to Rust-lifetime-misconceptions, s: Tr + 'a indicates that s can be one of the following:

  • a reference with exact lifetime 'a
  • an owned type whose lifetime outlives 'a with or without any lifetime parameters

so we can conclude that 't outlives 's outlives 'a.

Is my understanding correct?

The meaning of impl Tr + 'a is that all lifetimes annotated on the type must be larger than or equal to 'a. So we have 't larger than or equal to 'a in this case.

5 Likes