[Solved, Newbie question] About lifetimes

On the stack.

For the second thing. I'm not sure exactly what your question is. When you borrow value, the compiler creates a lifetime marker to track the relationship between our value, and the reference to it we just created.

The borrow-checker accepts the program if and only if it is possible to pick a region 'a such that:

  1. The value we borrowed from is valid inside the entirety of the region for 'a.
  2. All variables annotated with 'a exist entirely inside the region for 'a.

So the first gives value ≥ 'a, and the second gives 'a ≥ &'a value as well as 'a ≥ MyStruct<'a> and so on.

Sorry for answer time. First, thanks for all the time you spend for helping me ! :slight_smile:

I've a last question about lifetimes : when I annotate a function like this :
fn function<'a>(s: &'a type1, q:&'a type2) { }

Do I assume something about the arguments ? The first think I had it's : "must be a region where the arguments exist same time", is it right ?

Be reusing the same lifetime, you require that the references have the same lifetime. However in this case, that is fully general because if you have two different lifetimes, you can just replace that with the intersection of them and it'll work.

The trouble comes when you also return something. These are not the same, and can do different things:

fn function<'a>(a: &'a str, b: &'a str) -> &'a str { ... }
fn function<'a, 'b>(a: &'a str, b: &'b str) -> &'a str { ... }

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.