Ownership and borrowing

The borrow checker shapes every other decision in the language, so it comes before traits and generics. These twelve lessons take the rules in order: one owner per value and the move that ends the old binding, Copy and Clone when a copy is wanted, and Drop when a scope ends. The reference follows, &T and &mut T, the aliasing rule they share and the slices that borrow a part of a value. Lifetimes and elision explain what the compiler needs, then the chapter builds values of its own: a struct and its impl, constants and interior mutability, recursion, and the patterns a match destructures.

011 Ownership and moves one owner at a time, a move that invalidates the old binding, E0382 and the scope that ends an owner (C++ 031, 033) Open 012 Copy, Clone and explicit duplication Copy types duplicated on assignment, Clone on purpose, derive(Clone) and what a deep copy costs (C++ 031, 032) Open 013 Drop, scope and RAII the value is released when its scope ends, the Drop trait, a guard, and a release that survives an early return (C++ 016, 027) Open 014 Shared references &T as a borrow that never owns, references as parameters, no null reference, deref and method auto-ref (C++ 011) Open 015 Mutable references and aliasing &mut, one writer or many readers, E0499 and E0502, and the borrow that ends where it is last used (C++ 012) Open 016 Slices of arrays and strings &[T] and &str as views, ranges over a slice, split, and a function that takes a slice instead of a Vec (C++ 018, 020) Open 017 Lifetimes and elision one named lifetime, the three elision rules, a struct that holds a borrow and E0106 read in full (C++ 016) Open 018 Structs and field initialisation named fields, field init shorthand, struct update syntax, tuple structs and Default (C++ 024, 026) Open 019 impl, self and associated functions methods on &self, &mut self and self, associated functions as constructors, several impl blocks (C++ 024, 028) Open 020 const, static and interior mutability const items, static values, why mutation is not shared by default and the Cell and RefCell answer (C++ 012, 029) Open 021 Recursion and the stack a base case, the call stack, a recursive walk, a depth limit and a stack overflow read in gdb (C++ 014) Open 022 Patterns and destructuring let patterns, match arms with guards, binding with @, destructuring structs and tuples (C++ 021, 060) Open

Lessons