Comments (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.
Thanks for your elaborate answer!
from rust-blog.
Related Issues (20)
- Is it worth to mention about "Polonious"? HOT 1
- Remove static mut example HOT 8
- subjective issue, idk what title to give HOT 3
- A potential tweak to common-rust-lifetime-misconceptions HOT 1
- Sized is not an auto trait HOT 1
- PartialEq example isn't right HOT 2
- Paragraph about `T: 'static` is not right HOT 8
- Mention that Read & Write aren't in the prelude when talking about Generic blanket impls HOT 4
- My benchmark results HOT 5
- (nit) Update description of example provided to explain the Clone trait. HOT 1
- Not PostgreSQL but PostgresQL on purpose? HOT 1
- Request for additional example in closure/function lifetime elision distinction HOT 2
- `asymmetry` requirements in `PartialOrd`
- Customer review for Rust on the eve of 2022
- Misconception discussion: T is a superset of both &T and &mut T
- Struct methods can also NOT have elided lifetimes right?
- Request to add extra info: cannot implement both From and TryFrom
- More iteration trait wisdom
- tnanks the series high quality Rust article
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rust-blog.