Coder Social home page Coder Social logo

Comments (2)

pretzelhammer avatar pretzelhammer commented on May 22, 2024 2

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!

from rust-blog.

towry avatar towry commented on May 22, 2024

Thanks for your elaborate answer!

from rust-blog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.