120 cards
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, and the value is dropped when that owner goes out of scope. The "drop" here is not a casual phrase — it means the standard library's drop...
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 any attempt to use it is rejected at compile time. This design avoids double-free bugs that would otherwise arise if two bindings each believ...
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, while a mutable reference &mut T permits exclusive write access. The cornerstone rule — sometimes called "aliasing XOR mutability" — is th...
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 function signatures, struct definitions, and trait bounds. A reference such as &'a i32 is one that lives at least as long as the lifetime 'a. T...
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 pointer plus a length, without owning the underlying storage. They are constructed with range syntax: [..] takes the whole sequence, [a..] from...
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 needed when the borrow checker cannot statically prove that a mutation is safe, but you can still guarantee it through other means such as single...
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> increments a reference count, dropping one decrements it, and the inner value is freed when the count reaches zero. Because the count i...
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 to take it by shared reference, or ref mut to take it by mutable reference. Rust 1.26 introduced match ergonomics, so when you match on a re...