10 Lifetimes and interior mutability
This chapter covers
- Types of
&str
(there’s more than one) - Using lifetime annotations to help the compiler know how long a reference lives
- Using interior mutability, which provides safe mutability without
&mut
It is now time to learn about Rust’s famous lifetimes, used by the compiler to know when variables can be dropped and how long references last. Usually, you don’t need to specify lifetimes in your code, but sometimes the compiler needs a bit of help and will ask you to tell it how long something should last. We are also going to learn how to (safely!) mutate values without needing a mutable reference to do it!
10.1 Types of &str
We’ve been using &str
for most of the book so far. But here’s an interesting ...
Get Learn Rust in a Month of Lunches now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.