mirror of
https://github.com/darkicewolf50/RustBrock.git
synced 2025-07-07 03:27:13 -06:00
finished ch10
This commit is contained in:
18
Lifetimes.md
18
Lifetimes.md
@ -442,3 +442,21 @@ There are two input lifetimes, so Rust applies the first lifetime elision rule a
|
||||
Then because, because one of the parameters is `&self` the return type gets the lifetime of `&self` and all lifetimes have been dealt with
|
||||
|
||||
## The Static Lifetime
|
||||
One special lifetime is the `'static`, which denotes that the affected reference *can* live for the entire duration of the program
|
||||
|
||||
All string literals have the `'static` lifetime always
|
||||
|
||||
Here is how we can annotate it
|
||||
```rust
|
||||
let s: &'static str = "I have a static lifetime.";
|
||||
```
|
||||
|
||||
The string literal is stored in the program's binary which is always available
|
||||
|
||||
You might see suggestions to use `'static` lifetime in the error message.
|
||||
|
||||
Think about whether or not the value being referenced/the reference will always be valid before adding it
|
||||
|
||||
Most of the time the suggestion comes from attempting to create a dangling reference or a mismatch of the available lifetimes, instead of adding a `'static` lifetime annotation
|
||||
|
||||
Instead the solution is to fix those problems
|
Reference in New Issue
Block a user