Rust by Example, page by page¶
Level: reference · the coverage map
One line: Rust by Example ↗ is the syntax dictionary most Rust learners keep open in a tab; this page maps its 24 chapters onto the lessons here, says which ones this library goes deeper on, and names the ones it does not cover at all.
How to read this¶
RBE and this library answer different questions, so the map is not a race. RBE shows what the syntax is, in a runnable block, with two sentences around it. A page here picks one idea, shows the mistake that makes it worth knowing, and backs every printed number with a program CI compiles and runs. Where a row says covered, it usually means the lesson here is several times longer than the RBE page and about the trap rather than the form.
| meaning | |
|---|---|
| ✓ | a lesson here covers this, and goes further |
| ◐ | partly covered — the note says which half is missing |
| · | not covered here; read RBE, or the link in the note |
A few rows name a page in bold with no link, marked (in flight). Those are lessons another session has written and not yet committed — the link is left off deliberately, because a link to an uncommitted page fails the docs build for everybody. They become links in the commit that lands them.
Three whole chapters are deliberately · and always will be: Cargo, Crates and Meta are toolchain topics, and this library keeps those in Tooling organised by the problem you have rather than by the feature.
1. Hello World¶
| RBE page | Here | Note |
|---|---|---|
| Hello World ↗ | ✓ | Running a scratch program — rustc alone, cargo new, and the edition flag Cargo passes for you |
| Comments ↗ | ✓ | Comments that compile — //, ///, //!, and the two error codes a misplaced doc comment produces |
| Formatted print ↗ | ✓ | The format mini-language — width, precision, alignment, {:?}, {:#?} |
| Debug ↗ · Display ↗ | ✓ | Debug and Display — which one is for you and which is for your user, and why one is derived and the other never is |
| Formatting ↗ | ✓ | The format mini-language |
2. Primitives¶
| RBE page | Here | Note |
|---|---|---|
| Primitives ↗ | ✓ | Values (in flight) — the scalar types, and the width that runs out |
| Literals and operators ↗ | ✓ | Why hexadecimal, Meet the byte |
| Tuples ↗ | ✓ | Tuples — the anonymous struct, destructuring, and the arity where it stops being readable |
| Arrays and Slices ↗ | ✓ | Arrays and slices — [T; N] is a type per length; &[T] is the one you write in signatures |
3. Custom Types¶
| RBE page | Here | Note |
|---|---|---|
| Custom Types ↗ | ✓ | Structs and Enums are a section each |
| Structures ↗ | ✓ | What a struct is, then seven more: impl blocks, struct update, Copy vs Clone, the newtype |
| Enums ↗ | ✓ | What an enum is, variants that carry data, an enum as a state machine |
| use ↗ | ✓ | Bringing names in with use |
| C-like ↗ | ✓ | What an enum is — discriminants, and the as cast that reads one |
| Testcase: linked-list ↗ | ✓ | Nullable pointers and a generic recursive type — the same list, built twice |
| constants ↗ | ✓ | const and static — inlined at every use, versus one address for the whole program |
4. Variable Bindings¶
| RBE page | Here | Note |
|---|---|---|
| Variable Bindings ↗ | ✓ | Variables (in flight) |
| Mutability ↗ | ✓ | Variables (in flight) — mut is a property of the binding, not the type |
| Scope and Shadowing ↗ | ✓ | Five pages, collected in SHADOWING.md — including the three ways a shadow silently produces a wrong answer |
| Declare first ↗ | ✓ | Initial values — and why you almost never want to |
| Freezing ↗ | ◐ | The shadow-freezes-the-outer-binding trick appears in when to shadow; RBE's framing of it is not repeated |
5. Types¶
| RBE page | Here | Note |
|---|---|---|
| Types ↗ | ✓ | What a type annotation does |
| Casting ↗ | ✓ | Casting with as — the four silent losses, and the two conversions that are not casts |
| Literals ↗ | ✓ | Values (in flight) |
| Inference ↗ | ✓ | Type inference (in flight), and when the compiler cannot infer |
| Aliasing ↗ | ✓ | The Result you are reading is probably an alias — the alias you meet before you write one |
6. Conversion¶
| RBE page | Here | Note |
|---|---|---|
| From and Into ↗ | ✓ | From and Into — write one, get the other free |
| TryFrom and TryInto ↗ | ✓ | TryFrom and TryInto — the conversion that is allowed to say no |
| To and from Strings ↗ | ✓ | Parsing out of a string and Display — implement Display, never ToString |
7. Expressions¶
| RBE page | Here | Note |
|---|---|---|
| Expressions ↗ | ✓ | A block is an expression — the semicolon that turns a value into () |
8. Flow of Control¶
| RBE page | Here | Note |
|---|---|---|
| if/else ↗ · loop ↗ · while ↗ · for ↗ · match ↗ | ◐ | The Control flow (in flight) section is stubs — outlines with their traps written down, no runnable example yet |
| Nesting and labels ↗ · Returning from loops ↗ | ◐ | Stubs: break and continue, loop |
Destructuring ↗ — tuples, slices, enums, ref, structs |
◐ | One arm, many values does the enum-and-tuple half; slices and ref are not covered |
| Guards ↗ | ◐ | Zero wins is not zero games is about a guard that lets the wrong case through |
| Binding ↗ | · | The @ binding is not covered |
| if let ↗ | ✓ | if let — and the arm you deleted |
| let-else ↗ | · | Not covered |
| while let ↗ | ✓ | while let |
9. Functions¶
| RBE page | Here | Note |
|---|---|---|
| Functions ↗ | ◐ | Functions is a stub; the returned-value half is a block is an expression |
| Associated functions & Methods ↗ | ✓ | impl blocks and a type is not a constructor |
| Closures ↗ · Capturing ↗ | ✓ | What a closure is, the move keyword |
| As input parameters ↗ · Type anonymity ↗ | ✓ | The three closure traits — Fn, FnMut, FnOnce, and which one you are asking for |
| Input functions ↗ · Higher Order Functions ↗ | ✓ | Function pointers (in flight) |
| As output parameters ↗ | ✓ | Returning a trait — impl Fn versus Box<dyn Fn> |
Examples in std ↗ — Iterator::any, searching |
✓ | Iterators are lazy, iter, iter_mut, into_iter |
| Diverging functions ↗ | ◐ | ! turns up in what a panic costs; the never type is not its own page |
10. Modules¶
| RBE page | Here | Note |
|---|---|---|
| Modules ↗ · Visibility ↗ | ✓ | Modules and visibility — private by default, and what "private" is measured against |
| Struct visibility ↗ | ✓ | Modules and visibility, and the door it leaves open in the newtype |
| The use declaration ↗ | ✓ | Bringing names in with use — and a trait must be in scope, which is the version that bites |
| super and self ↗ | ✓ | Modules and visibility |
| File hierarchy ↗ | ✓ | One module per file — mod.rs versus name.rs, and the declaration that is not an include |
11. Crates¶
| RBE page | Here | Note |
|---|---|---|
| Crates ↗ · Creating a Library ↗ · Using a Library ↗ | ◐ | One module per file covers the crate root and the two crate kinds; the rustc --crate-type and --extern mechanics are RBE's, and Adding a dependency is what you actually do |
12. Cargo¶
| RBE page | Here | Note |
|---|---|---|
| Dependencies ↗ | ✓ | Adding a dependency — search, info, add, and what the caret permits |
| Conventions ↗ | ✓ | Running a scratch program and scaffolding a practice tree |
| Testing ↗ | ✓ | Where a test goes |
| Build Scripts ↗ | · | Not covered |
13. Attributes¶
| RBE page | Here | Note |
|---|---|---|
| Attributes ↗ | ✓ | What an attribute is |
| dead_code ↗ | ✓ | What a warning is asking — including which of the four fixes is right |
| cfg ↗ | ✓ | What an attribute is — and #[cfg(test)], which is the one you meet first, in where a test goes |
Crates (crate_type) ↗ |
· | Superseded by Cargo; not covered |
14. Generics¶
| RBE page | Here | Note |
|---|---|---|
| Generics ↗ · Functions ↗ · Implementation ↗ | ✓ | What a generic is |
| Traits ↗ · Bounds ↗ · Multiple bounds ↗ · Where clauses ↗ | ✓ | Where the bound goes |
| New Type Idiom ↗ | ✓ | A score is not a number |
| Associated items ↗ · Associated types ↗ | ✓ | Implementing Iterator — type Item is the associated type you meet first |
| Phantom type parameters ↗ | ✓ | Phantom types |
| Testcase: empty bounds ↗ | ✓ | Marker traits |
15. Scoping rules¶
| RBE page | Here | Note |
|---|---|---|
| RAII ↗ · Destructor ↗ | ✓ | Drop, and what RAII buys |
| Ownership and moves ↗ · Mutability ↗ | ✓ | Ownership and moves |
| Partial moves ↗ | ✓ | Struct update syntax, and the partial move |
| Borrowing ↗ · Mutability ↗ · Aliasing ↗ | ✓ | Borrowing — and where a borrow ends, which RBE does not say |
| The ref pattern ↗ | · | Largely obsolete since match ergonomics; not covered |
| Lifetimes ↗ · Explicit annotation ↗ · Functions ↗ · Structs ↗ | ✓ | Lifetime annotations, and how to learn lifetimes first |
| Static ↗ | ✓ | &'static str — the two meanings of 'static, which is the trap |
| Elision ↗ | ✓ | Lifetime annotations |
16. Traits¶
| RBE page | Here | Note |
|---|---|---|
| Traits ↗ | ✓ | What a trait is |
| Derive ↗ | ✓ | What an attribute is — derive is the attribute you use most |
| Returning Traits with dyn ↗ | ✓ | Returning a trait, static vs dynamic dispatch |
| Operator Overloading ↗ | ✓ | Operators are traits — + is Add::add, and the newtype that gets one |
| Drop ↗ | ✓ | Drop, and what RAII buys |
| Iterators ↗ | ✓ | Implementing Iterator |
| impl Trait ↗ | ✓ | Returning a trait |
| Clone and Copy ↗ | ✓ | Copy vs Clone, ToOwned, clone_into |
| Supertraits ↗ | ✓ | Supertraits |
| Disambiguating overlapping traits ↗ | ◐ | The fully-qualified call form appears in a trait must be in scope |
17. Macros¶
| RBE page | Here | Note |
|---|---|---|
| macro_rules! ↗ · Syntax ↗ · Designators ↗ · Overload ↗ · Repeat ↗ | ◐ | Macros is a stub — what the ! means, and the three things a macro can do that a function cannot |
| DRY ↗ · DSLs ↗ · Variadics ↗ | · | Not covered |
18. Error handling¶
This is the chapter this library covers most heavily: two sections, thirty-odd pages, and a map of its own in OPTION.md.
| RBE page | Here | Note |
|---|---|---|
| panic ↗ · abort and unwind ↗ | ✓ | What a panic costs — the damage it does halfway through a job |
| Option & unwrap ↗ | ✓ | Some and None, unwrap is a TODO, expect |
| Unpacking options with ? ↗ · Introducing ? ↗ | ✓ | main can return a Result |
| Combinators: map ↗ · and_then ↗ | ✓ | map_or, Option is a one-item collection, what a monad is |
| Unpacking options and defaults ↗ | ✓ | Four pages: unwrap_or, unwrap_or_else, unwrap_or_default, map_or |
| Result ↗ · Using Result in main ↗ | ✓ | Option vs Result, main can return a Result |
| aliases for Result ↗ | ✓ | The Result you are reading is probably an alias |
| Multiple error types ↗ · Defining an error type ↗ · Boxing errors ↗ · Wrapping errors ↗ | ◐ | Not every error is an io::Error and thiserror vs anyhow are stubs today |
| Iterating over Results ↗ | ◐ | Keep going, or stop — stub, same four strategies |
19. Std library types¶
| RBE page | Here | Note |
|---|---|---|
| Box, stack and heap ↗ | ✓ | Stack and heap, then Box — one value, moved off the stack |
| Vectors ↗ | ✓ | Vec — three numbers, and the reallocation you can see |
| Strings ↗ | ✓ | Twenty-one pages in Strings, mapped in STRINGS.md |
| Option ↗ · Result ↗ · ? ↗ · panic! ↗ | ✓ | Option and Result, the largest section here |
| HashMap ↗ · Alternate/custom key types ↗ | ✓ | HashMap — entry, and what a key has to promise |
| HashSet ↗ | ✓ | HashSet |
| Rc ↗ · Arc ↗ | ✓ | Rc, Arc |
20. Std misc¶
| RBE page | Here | Note |
|---|---|---|
| Threads ↗ · Testcase: map-reduce ↗ | ✓ | Spawning a thread — and thread::scope, which RBE does not cover; then Arc and lock poisoning |
| Channels ↗ | ✓ | Channels — mpsc, the Sender you kept, and bounded backpressure |
| Path ↗ | ◐ | Path and PathBuf — stub |
| File I/O ↗ · open ↗ · create ↗ · read_lines ↗ | ◐ | Files — five stubs, including reading lines efficiently, which is RBE's "more efficient approach" |
| Child processes ↗ · Pipes ↗ · Wait ↗ | · | Not covered |
| Filesystem Operations ↗ | · | Not covered |
| Program arguments ↗ · Argument parsing ↗ | ◐ | Command line — six stubs, including clap, which is what you should actually use |
| Foreign Function Interface ↗ | · | Named as future work in Advanced |
21. Testing¶
| RBE page | Here | Note |
|---|---|---|
| Unit testing ↗ | ✓ | What a test asserts — and the assertion that passes for the wrong reason |
| Documentation testing ↗ | ✓ | The example that is a test |
| Integration testing ↗ | ✓ | Where a test goes — #[cfg(test)] beside the code, versus tests/ outside it |
| Development dependencies ↗ | ✓ | Where a test goes, and cargo-nextest |
22. Unsafe Operations¶
| RBE page | Here | Note |
|---|---|---|
| Unsafe Operations ↗ | ✓ | What unsafe turns off — the five powers, and the four rules that stay on; then a union and the global allocator as two of them in use |
| Inline assembly ↗ | · | Not covered |
23. Compatibility¶
| RBE page | Here | Note |
|---|---|---|
| Raw identifiers ↗ | · | Not covered — r#match is real and rare |
24. Meta¶
| RBE page | Here | Note |
|---|---|---|
| Documentation ↗ | ◐ | Comments that compile covers /// and //!; doc tests covers the part that runs |
| Playground ↗ | · | Not covered |
What is still missing¶
Ordered by what it costs a reader who came here from RBE:
- Flow of control has no runnable page.
25_Control_Flow/(in flight) is eight stubs, andif/match/forare the first things anyone needs. Highest-value gap in the library. macro_rules!— one stub, and RBE's five pages are the best short treatment anywhere.let-else,@bindings, slice patterns. Pattern matching beyond one arm.- Child processes, filesystem operations, FFI. All three are stubs or absent.
See also¶
- Rust by Example — what RBE is for, and the two moments it is the right answer
- The course, in order — the reading order these lessons were written in
- KATAS.md — the exercises, which RBE calls Activities
Po polsku¶
Rust by Example to zbiór krótkich, uruchamialnych przykładów, który większość uczących się trzyma otwarty w drugiej karcie przeglądarki. Rzecz, którą lepiej wiedzieć od razu: polskiego tłumaczenia nie ma. Oficjalne repozytorium prowadzi przekłady na hiszpański, japoński, koreański i chiński — i na tym koniec. Nie przeoczyłeś polskiej wersji, ona nie istnieje.
Ta strona jest mapą pokrycia: przechodzi przez 24 rozdziały RBE i mówi, która lekcja tutaj im odpowiada, gdzie ta biblioteka schodzi głębiej, a czego nie porusza wcale. Korzystać z niej warto w jedną konkretną stronę — RBE odpowiada na pytanie „jak się to zapisuje", a lekcje tutaj na pytanie „dlaczego tak i gdzie to boli". Role są komplementarne, nie konkurencyjne.
Stąd rada praktyczna dla uczącego się po polsku, i to ta sama, którą powtarza POLSKI.md: składni ucz się, skąd chcesz, ale komunikatów kompilatora ucz się po angielsku. rustc po polsku nie mówi i mówić nie będzie, a większość czasu w Ruscie spędza się na czytaniu tego, co wypisał.
Szukaj po polsku: kurs Rusta po polsku · dokumentacja Rusta · rust by example · rust book tłumaczenie