Ownership & borrowing
15 steps in 6 sets of Rust.
The tour where Rust stops being familiar. Move semantics first, then borrowing, then the slices and string types that exist because borrowing does. Structs and methods, and finally the smart pointers for when a single owner is genuinely the wrong model.
Fifteen steps that everyone finds harder than the count suggests. The borrow checker is not being difficult for the sake of it, and the moment that clicks is the moment Rust starts paying you back.
Move & clone
- Ownership and moveEvery value has one owner; passing it moves it.
- Clone and CopyCopy types duplicate silently; everything else moves unless you ask for a clone.
- Ownership across functionsA function can take ownership, borrow, or hand ownership back.
Borrowing
- BorrowingReferences let you use a value without owning it.
- Mutable borrowsOne mutable borrow at a time, and never alongside a shared one.
- iter, iter_mut, into_iterThree ways to walk a collection: by reference, by mutable reference, by value.
Slices & strings
- SlicesBorrowing a contiguous part of a string or array.
- Reverse a stringReverses by chars, because a Rust String is UTF-8 and bytes are not characters.
- String and &strThe owned String versus the borrowed &str.
- Taking &str, returning StringThe API convention: borrow the cheapest thing that works, own what you return.
Structs & methods
- Methods and implAttaching behavior to a struct with an impl block.
- Struct with implA struct and its impl block, where Rust puts the methods.
Smart pointers
- Box and recursive typesHeap allocation that makes a recursive enum sized.
- Rc shared ownershipReference counting for multiple owners of one value.
Encore
- rpn.rsEvaluate a reverse-Polish expression with a Vec stack.