finished ch10

This commit is contained in:
2025-02-05 11:14:05 -07:00
parent 89f9705f62
commit b00b78a773
4 changed files with 75 additions and 14 deletions

View File

@ -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