May 2019
Intermediate to advanced
698 pages
17h 21m
English
For cases where the compiler can't figure out the lifetime of values by examining the code, we need to tell Rust by using some annotations in code. To distinguish from identifiers, lifetime annotations are denoted by a quirky symbol of prefixing a letter with '. So, to make our previous example compile with a parameter, we have added a lifetime annotation on our StructRef, like so:
// using_lifetimes.rsstruct SomeRef<'a, T> { part: &'a T}fn main() { let _a = SomeRef { part: &43 };}
A lifetime is denoted by a ', followed by any sequence of valid identifiers. But, by convention, most lifetimes used in the Rust code uses 'a, 'b and 'c as lifetime parameters. If you have multiple lifetimes on a type, you can use longer descriptive ...
Read now
Unlock full access