What does the first explicit life-cycle specifier on impl mean?

There are three different lifetime specifiers for impl:

impl<'a> Type<'a> { fn my_function(&self) -> &'a u32 { self.x } } 

Type<'a> indicates that a lifetime exists in this impl declaration. One of the return type -> &'a u32 states that a variable that receives the return value should not die earlier ... to what? Before an object of type Type fades out?

Who cares:

 impl TextEditor { //Other methods omitted ... pub fn get_text<'a>(&'a self) -> &'a String { return &self.text; } } 

Here he says that the return type does not die before the expiration of the &'a self .

Does the last one declare the last time to this method alone, and does the other declare the lifetime of each method (and associated function?) In the impl declaration?

+5
source share
2 answers

'a is the lifetime parameter in both cases. This is a kind of general parameter, so each use of Type or each use of get_text can choose a different "value" for this general parameter. Actual lifetimes are never chosen by an explicit programmer, unless you use 'static .

The compiler will output what 'a should be for each Type value or for every use of get_text .

impl<'a> introduces a new lifetime parameter for the entire impl block. Then it is used in the type: impl<'a> Type<'a> { .. }

Exactly what 'a means depends on how it is used in the Type definition. From your example, I think Type :

 struct Type<'a> { x: &'a u32, } 

This definition reads: For each 'a lifetime, define a type containing the reference x: &'a u32 . Thus, Type is generic and can store a link with any lifetime.

impl<'a> Type<'a> { .. } reads: For each lifetime 'a define methods for the type Type<'a> .

Since we now know the definition of the Type structure, we know that the 'a parameter inside the impl block is always equal to the link lifetime in the Type x field.

One that has a return type โ†’ & 'a u32 indicates that a variable that receives the return value should not die earlier ... to what? Before a Type object dies?

'a is the lifetime of the link stored inside the Type<'a> value, and it has nothing to do with the type value itself. The only rule is that 'a must survive the value of the type itself, because it is not allowed to store the link beyond its end. So we can stick with this &'a u32 until at least the point where the Type value dies and maybe more.


 impl TextEditor { //Other methods omitted ... pub fn get_text<'a>(&'a self) -> &'a String { return &self.text; } } 

This is really common. &self is a reference to the value self - borrowing, and the get_text method get_text again a common element. It has one common parameter - the parameter of the lifetime.

It reads for any lifetime, 'a borrows itself as a link &'a self (a reference to this lifetime) and returns a reference to a String with the same lifetime.

Using the same parameter both on input &self and on output &String means that they are connected, and Rust will consider itself as borrowed until the returned reference to String is returned.

Again, the get_text method is generic, and the compiler will select a 'value' for 'a for each use of the method. This is a method that can return differently long string entries, depending on how long you let it take self . Sometimes it chooses a long life, so you can hold the returned &String long time. In some cases, using get_text will use a shorter lifetime if you do not use the return value at all.

In this case, since we see that &'a String tied directly to borrowing &'a self the TextEditor value, we know that we can only support a reference to String for as long as we can hold the borrow value of TextEditor .

+6
source

To paraphrase a rust code:

 impl<'a> 

"If you give me your whole life ..." (the compiler usually delivers this based on context when using the type)

  Type<'a> { 

"... I will describe how to implement Type<'a> ". Thus, Type probably contains references (which require a lifetime).

  fn my_function(&self) -> &'a u32 { 

"... and specifying a link to Type<'a> , you call my_function() to get a link to u32 with a lifetime of 'a ." Note that the lifetime of the &self link is not directly tied to 'a ; it may be shorter (but usually no longer than 'a , since the type cannot survive the contained links).

In the second case:

 impl TextEditor { 

"Here, how to implement the inanimate parametric type TextEditor ..."

 pub fn get_text<'a> 

"Given the lifetime 'a , which you can select (this is an input parameter) ..."

  (&'a self) 

"... and a link to TextEditor , which lives at least 'a .."

  -> &'a String { 

"... you can call the get_text method and get a borrowed reference to a String that lives at the same time."

In a more practical sense, this really means that the String reconfigured directly from the TextEditor - if this String link is active, the &self loan is still considered active, and you wonโ€™t be able to accept any &mut links.

+5
source

All Articles