← All lessons
Rust
All Rust snippets · 34
Every snippet has its own focus page with an explanation and the output it produces.
Variables & Types · 3
- Immutable By DefaultBindings cannot be reassigned unless declared mut. Immutability is the default so shared data is safe to reason about.
- Shadowing Changes TypeRe-using let creates a brand new binding that may have a different type. This is shadowing, not mutation.
- Explicit Integer TypesRust integers carry their width and signedness in the type. Suffixes like _f32 annotate a literal directly.
Ownership · 3
- Values MoveAssigning a heap-backed value moves ownership. s1 is invalid afterwards, which is what stops two owners freeing the same memory.
- Cloning To Keep Bothclone makes a deep copy so both bindings own their own data. It is explicit because the allocation cost should be visible.
- Copy Types Do Not MoveIntegers and other stack-only types implement Copy, so assignment duplicates them and the original stays usable.
Borrowing · 3
- Borrowing With &A reference lends access without transferring ownership, so the caller can keep using the value after the call.
- One Mutable Borrow At A TimeYou may hold either many shared borrows or exactly one mutable borrow. The inner scope ends the mutable borrow before v is read again.
- String SlicesA slice borrows a contiguous range rather than copying it. &str is the borrowed counterpart of an owned String.
Control Flow · 3
- If Is An Expressionif produces a value, so it can sit on the right of a let. Both branches must have the same type.
- Loop With Break Valueloop runs until an explicit break, and break can carry a value out, which while and for cannot do.
- For Over A Range0..5 excludes the end, 1..=10 includes it. Ranges are iterators, so adapters like sum work on them directly.
Pattern Matching · 3
- Match Must Be Exhaustivematch requires every possible value to be covered. Arms support alternatives with | and inclusive ranges with ..=.
- Destructuring In MatchPatterns can pull fields out of structs and match on their values at the same time, binding the rest to new names.
- If Let For One Caseif let is shorthand when only one pattern matters, avoiding a match with a throwaway _ arm.
Enums · 2
Error Handling · 3
- Result For Fallible WorkResult carries either a success value or an error. Nothing forces you to unwrap, so failure paths stay visible in the types.
- Propagating With ?? returns the error early on failure and unwraps the value on success, collapsing a match into a single character.
- Unwrap Panicsunwrap aborts the program on None or Err. Prefer unwrap_or, unwrap_or_else or a match anywhere failure is possible.
Structs · 2
Traits · 2
Collections · 2
Iterators · 3
- Iterator ChainsAdapters build a pipeline description; nothing runs until a consumer such as collect drives it. The whole chain compiles to one loop.
- Iterators Are LazyBuilding an adapter does no work. Forgetting the consumer is a common bug because the compiler only warns rather than erroring.
- Folding To One Valuefold threads an accumulator through every element, returning it at the end. It generalises sum, product and count.