pretzelhammer / rust-blog

Educational blog posts for Rust beginners
Apache License 2.0
7.11k stars 380 forks source link

Confuse: lifetime with struct and self. #22

Closed towry closed 3 years ago

towry commented 3 years ago

https://github.com/pretzelhammer/rust-blog/blob/master/posts/common-rust-lifetime-misconceptions.md

#[derive(Debug)]
struct NumRef<'a>(&'a i32);

impl<'a> NumRef<'a> {
    // no more 'a on mut self
    fn some_method(&mut self) {}

    // above line desugars to
    fn some_method_desugared<'b>(&'b mut self){}
}

fn main() {
    let mut num_ref = NumRef(&5);
    num_ref.some_method();
    num_ref.some_method(); // compiles
    println!("{:?}", num_ref); // compiles
}

It seems you simply give the solution (let compiler handle the lifetime). but why? what the lifetime 'a in struct NumRef doing/restricting? what is lifetime 'a in fn main ? (desugar lifetime in fn main maybe helpful?)

Thanks.

pretzelhammer commented 3 years ago

what the lifetime 'a in struct NumRef doing/restricting?

That explicit lifetime annotation is required by the compiler. The compiler does not perform any lifetime inference or elision in struct definitions, so all lifetime must be explicitly annotated. The lifetime annotation itself is not doing anything special, it doesn't make the struct any more or less restrictive than it already is, it's simply there because it's required to be there.

what is lifetime 'a in fn main ? (desugar lifetime in fn main maybe helpful?)

The lifetime is 'static because the type of the literal value &5 is &'static i32 so NumRef(&5) would have the type NumRef<'static>, desugared example:

fn main() {
    let mut num_ref: NumRef<'static> = NumRef(&5); // compiles
    num_ref.some_method();
    num_ref.some_method(); // compiles
    println!("{:?}", num_ref); // compiles
}

On the other hand, if we don't use the &5 literal value anywhere, but just declare some number 5 and create a reference to it later then the lifetime of the reference persists until the end of the block scope it was created in. Desugared example:

fn main() {
    let num = 5;
    // &num has some anonymous lifetime that starts on the next line and ends at the last line of main function
    let mut num_ref: NumRef<'_> = NumRef(&num); // compiles
    num_ref.some_method();
    num_ref.some_method(); // compiles
    println!("{:?}", num_ref); // compiles
    // &num's and num_ref's lifetimes end here
}

The syntax '_ asks the compiler to infer the appropriate lifetime based on context, we had to use this syntax in the above example because all lifetimes are anonymous and don't have names outside of generic contexts. The only exception is 'static which is the only lifetime with a name that can be used outside of generic contexts.

I'm not sure if I answered your question. Please let me know if I was able to help and if you have any follow-up questions!

towry commented 3 years ago

Thanks for your elaborate answer!