First vs second lifetime annotation in impl

I know this is covered in the book somewhere but I couldn't find it at a glance...

When defining lifetimes on an impl, what is the difference between the first and the second lifetime annotation?

I mean, something like this is everywhere, but I don't know the difference between the two 'a's:

impl <'a> Foo for Bar <'a> { ...}

1 Like

Here the link into the rust book.
https://doc.rust-lang.org/1.9.0/book/lifetimes.html

2 Likes

Oh - thanks... so the first is like the declaration of the generic, and the second is the actual bound for the trait?

1 Like

Yes it's about that. After that does not serve often. These are very specific cases but when we fall on it we are happy to know.

1 Like

The first 'a represents a generic lifetime (parameter), it will assume a certain lifetime that can be constrained by where: conditions.
The second 'a represents a generic argument, a hole that's filled with the concrete lifetime that the former 'a assumes on instantiation of the block, basically.
Instantiation is maybe not the correct word, but I use it to convey "filling in all generic parameters".

Since the lifetime parameter is defined on the impl block, all implemented methods can use a generic argument (with the same name) to reference the concrete lifetime. The concrete lifetime will come from the calling context.
In this case Bar<'a>; 'a is a lifetime used within the Bar struct. This implies that the lifetime 'a will outlive the structure itself. This can be expressed as a constraint and is implicitly added by the compiler:

'a: Bar<'a>, = 'a: Self, -> 'a outlives Bar<'a>
equivalent to
lifetime 'a has the same-or-bigger scope than the instantiation of Bar<'a>.

Example code: Rust Playground

In Rust, everything has a lifetime.
When working with generics it's necessary to keep track of a certain lifetime so the compiler can verify soundness. That's where generic lifetime parameters come into play.

Edit: I had the bounds the other way around. Outlives relationship is defined as 'longer: 'shorter, see Bounds - Rust By Example.

2 Likes