liveBook · Manning

Chapter 10: Lifetimes and interior mutability

What you will learn in chapter 10 of Learn Rust in a Month of Lunches

The introduction explains why lifetime annotations may be needed and introduces interior mutability as a way to change values safely without a mutable reference.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction explains why lifetime annotations may be needed and introduces interior mutability as a way to change values safely without a mutable reference.

Key terms: lifetimes, lifetime annotations, references, interior mutability, &mut

10.1 Types of &str

This section distinguishes string literals from borrowed strings and explains why their lifetimes affect whether a function can return a reference to them.

Key terms: string literals, borrowed str, &'static str, &String, &str, reference, 'static lifetime, string literal

10.2 Lifetime annotations

Introduces lifetime annotations as extra information Rust sometimes needs and explains why references cannot outlive the objects they refer to.

Key terms: lifetime, lifetime annotation, reference

10.2.1 Lifetimes in functions

This section explains why a function cannot return a reference to a local String, when it can return a &'static str, and what lifetime annotations do.

Key terms: String, &str, 'static, string literal, lifetime annotation, &'static str, T: Display

10.2.2 Lifetime annotations in types

The section shows why a struct that stores a string reference needs a lifetime annotation, and why that annotation restricts accepted references rather than extending their lives.

Key terms: &str, reference, lifetime, 'static, &'static str, string literals, City<'a>, name: &'a str, lifetime annotation

10.2.3 The anonymous lifetime

This section explains the anonymous lifetime in implementations, shows how implementations relate lifetimes on traits and structs, and offers practical ways to work through lifetime errors.

Key terms: anonymous lifetime, <'_>, impl block, trait, struct, impl, elide the lifetimes, owned types, String, &'a str

10.3 Interior mutability

Introduces interior mutability as a way to change selected data inside an otherwise immutable struct, using a phone model as an example.

Key terms: interior mutability, mut, PhoneModel, on_sale, &self, &mut self

10.3.1 Cell

This section shows how Cell lets a PhoneModel change its on_sale value without a mutable reference and explains why Cell is best suited to simple Copy types.

Key terms: Cell<bool>, interior mutability, .set(), Cell, .get(), Copy

10.3.2 RefCell

Introduces RefCell as a way to change a value without declaring its owner mutable, explains its borrowing rules and runtime checks, and notes its limitation with multiple threads.

Key terms: RefCell, interior mutability, borrow_mut(), borrow(), immutable borrows, mutable borrow, run time, panic, try_borrow_mut(), double borrow

10.3.3 Mutex

This section explains how a Mutex permits controlled changes to a value, how its lock is released, and how to avoid waiting indefinitely for a lock.

Key terms: Mutex, mutual exclusion, .lock(), MutexGuard, drop(), deadlock, .try_lock(), if let, match

10.3.4 RwLock

Explains how RwLock permits read and write access, why conflicting access can deadlock, and how to avoid waiting forever.

Key terms: RwLock, read–write lock, .read(), .write(), deadlock, drop(), .try_read(), .try_write()

10.4 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 9: Iterators and closures again! Chapter 11: Multiple threads and a lot more →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.