Skip to content

Rust Ownership And Borrowing Explained Textbook

A free, self-paced textbook in 8 chapters. Read a chapter, then drill it with the 120 companion flashcards using spaced repetition.

8 chapters · 120 cards · Updated

Chapters

  1. 1Ownership Rules and Memory

    Rust's ownership system rests on three simple rules that govern every value in the language: every value has exactly one owner, there can only be a single owner at any given moment...

  2. 2Moves, Copy, and Clone

    When you assign a value to another variable or pass it to a function, Rust moves the value rather than copying it by default. After a move, the source binding is invalidated, and a...

  3. 3Borrowing and the Borrow Checker

    Borrowing is the act of letting another piece of code access your value without taking ownership, by handing out a reference. A shared reference &T permits read-only access, wh...

  4. 4Lifetimes, Variance, and PhantomData

    Lifetime annotations are the mechanism by which the borrow checker understands how multiple references relate in time. Written as generic parameters like 'a, they appear in functio...

  5. 5Slices, Strings, and Deref Coercion

    A slice is a reference to a contiguous sequence of elements, written &[T] for a shared slice or &mut [T] for a mutable slice. Slices always borrow data and consist of a poi...

  6. 6Interior Mutability and Runtime Enforcement

    Interior mutability is a family of patterns that allows mutation through a shared reference, with the borrowing rules enforced at runtime rather than at compile time. This is neede...

  7. 7Shared Ownership Across Threads

    When multiple parts of a program legitimately need to own the same data, Rust offers reference-counted smart pointers. Rc<T> is the single-threaded choice: cloning an Rc<T...

  8. 8Patterns, Iteration, and Unsized Types

    Pattern matching interacts with ownership in subtle but useful ways. By default, destructuring a struct or tuple in a let or match moves the fields out — prefix a binding with ref...

← Back to the Rust Ownership And Borrowing Explained deck