Rust in Action, run¶
Level: reference · a companion to the book
One line: Tim McNamara's Rust in Action (Manning, 2021) teaches systems programming through Rust. These pages run its listings on rustc 1.98.0, check the claims in the prose around them, and point each idea to the lesson in this library that covers it.
The book's code is in rust-in-action/code ↗, on the 1st-edition branch. That repository has no licence, so the listings are linked here rather than copied. Each page records what the book's file prints on rustc 1.98.0 and runs a program of its own for every claim it checks. For what this library thinks of the book, and how many of the repository's 62 Cargo projects still compile on 1.98.0, see Books.
Chapter 2 — Language foundations¶
The pages for sections with a lesson section of their own live there — Cargo in Tooling, comparison and other number types in Numbers, flow control in Control flow — and are listed here with the rest.
| Book | The book's file | Page here | What the page checks |
|---|---|---|---|
| before listing 2.2 | ch2/ok.rs ↗ | From one .rs file to a Cargo project |
rustc on one file, then cargo init and what cargo run -v shows |
| §2.2.1, listing 2.2 | ch2-first-steps.rs ↗ | Listing 2.2, run: variables and functions | inference from use; : i32, 30i32 and 30_i32; assigning later without mut; the value of a macro call; the semicolon note; what main may return |
| §2.3.1, listing 2.3 | ch2-intro-to-numbers.rs ↗ | Listing 2.3, run: integers and floats | the : i64 that is needed; an array's element type; {:02}; round; bits versus bytes; explicit conversions; + as a trait |
| §2.3.2, listing 2.4 | ch2-non-base2.rs ↗ | Listing 2.4, run: base 2, 8 and 16 | base prefixes; 036; 0x1f32; {:#x}; from_str_radix; two's complement |
| §2.3.2, tables 2.1 and 2.2 | — | Tables 2.1 and 2.2, run: numeric types and their bits | the bit patterns of 20; i128 and u128; special float values; what decides the width of usize |
| §2.3.3 | — | What Rust in Action §2.3.3 says about comparing numbers, run | fifteen claims: the tolerance example, comparing two number types, NaN, the CPU-exception footnote |
| §2.3.4, listing 2.6 | ch2/ch2-complex ↗ | Rust in Action §2.3.4, run · Other number types | complex numbers with the num crate, and the number types std does not have |
| §2.4, listings 2.7 and 2.8 | — | What Rust in Action says about flow control, run · Flow control | twenty-one claims from if to loop labels, one folder per construct |
What the pages found¶
All three listings compile and run on rustc 1.98.0 without a warning. The outputs of listings 2.3 and 2.4 match the book exactly. These are the places where the text and the compiler disagree, or where the text stops short:
| Where | The book, paraphrased | rustc 1.98.0 |
|---|---|---|
| §2.2.1, before listing 2.2 | the listing prints a + b = 30 |
it prints ( a + b ) + ( c + d ) = 90 |
| §2.2.1, the note | a semicolon after i + j makes add return () |
E0308: add no longer compiles |
| §2.2.1, p. 36 | let c = 30_i32; is line 5; fn add is line 10 |
let d = 30_i32; is line 5; fn add is line 11 |
| §2.2.1, p. 36 | a macro returns code rather than values | the code has a value: println! is () |
| §2.2.1, footnote | main returns () or a Result |
any Termination type, ExitCode included |
| §2.3.1 | you declare a type's size in bytes | the name counts bits: an i32 is 4 bytes |
| §2.3.1 | conversions between types are always explicit | true of numbers; &String to &str is a silent coercion |
| listing 2.3, line 9 | its note covers the underscores; nothing says the : i64 matters |
needed: E0689 without it, overflow as i32 |
| table 2.1 | integers from 8 to 64 bits | i128 and u128 too, stable since 1.26.0 |
| table 2.1 | isize and usize take the CPU's native width |
the pointer width: 32 bits on 64-bit x86_64-unknown-linux-gnux32 |
| §2.3.2 | unsigned integers represent only positive numbers | and zero |
| §2.3.2 | floats represent real numbers | a finite set of binary fractions: 0.1 is not among them |
Concept index¶
Every concept chapter 2 has shown so far: where the book shows it, which page here checks it, and the lesson that teaches it.
| Concept or keyword | In the book | Checked on | The lesson |
|---|---|---|---|
let, immutability |
listing 2.2, lines 2–5 | 2.2, claim 4 | Variables · The shadowing map |
| type inference, integer fallback | listing 2.2, line 2; listing 2.3, line 2 | 2.2, claim 2 · 2.3, claim 1 | Type inference |
| type annotation | listing 2.2, line 3; listing 2.3, lines 3 and 9 | 2.2, claim 3 · E0689 |
What a type annotation does |
literal suffix, _ separator |
listing 2.2, lines 4–5; listing 2.3 | 2.2, claim 3 | Writing a number down |
fn, parameters, -> |
listing 2.2, line 11 | 2.2, refusals | Functions |
tail expression, ;, () |
listing 2.2, line 12, and the note | 2.2, the semicolon | A block is an expression · The unit type () |
entry point, main |
listing 2.2, line 1, and footnote 1 | 2.2, main |
Running a scratch program |
macros, println!, {} |
listing 2.2, line 8 | 2.2, claim 5 · Display |
Macros · Debug and Display |
char versus string |
§2.2.1, p. 36 | 2.2, claim 8 | Meet the char |
| methods on numbers, overflow | listing 2.3, line 10 | 2.3, claim 2 | Meet the byte · Signed overflow |
| arrays, indexing | listing 2.3, lines 12–18 | 2.3, claims 3 and 9 | Arrays and slices |
| format spec: width, fill, base | listing 2.3, line 18; listing 2.4 | 2.3, claim 4 · 2.4, claim 5 | The format mini-language |
| rounding a float | §2.3.1 | 2.3, claim 5 | Making a float whole |
| conversions, coercion | §2.3.1 | 2.3, claim 7 | From and Into · Coercion · Casting with as |
| operator overloading | §2.3.1 | 2.3, claim 8 | Operators are traits |
| base 2, 8 and 16 | listing 2.4 | 2.4, claims 1–6 | Why hexadecimal |
| two's complement | table 2.2 | 2.4, claim 7 · tables, claim 4 | Meet the byte |
integer widths, i128 |
table 2.1 | tables, claims 2–3 | Values · What i128 is exact about |
isize, usize |
table 2.1 | tables, pointer width | Values |
IEEE 754 floats, NaN, -0.0 |
table 2.2 | tables, claims 1 and 5–7 | What a float actually stores |
The one-sentence definitions are in the glossary, and each error code these pages meet (E0277, E0284, E0308, E0384, E0601, E0689) has its lesson in the error index.
Po polsku¶
Rust in Action Tima McNamary (Manning, 2021) uczy programowania systemowego na przykładzie Rusta. Te strony uruchamiają listingi z książki na rustc 1.98.0, sprawdzają twierdzenia z tekstu wokół nich i odsyłają każde pojęcie do lekcji w tej bibliotece. Kod z książki jest tylko linkowany, nie kopiowany — repozytorium nie ma licencji.
Z rozdziału 2 wynika na razie tyle: listing 2.2 wypisuje = 90, a nie zapowiadane a + b = 30; średnik po i + j nie sprawia, że funkcja zwraca (), tylko powoduje błąd E0308; adnotacja : i64 w listingu 2.3 jest konieczna; nazwy typów liczą bity, nie bajty; tabela 2.1 pomija i128 i u128; a usize ma szerokość wskaźnika, nie procesora.
Szukaj po polsku: Rust in Action po polsku · podstawy języka Rust · literały liczbowe · wnioskowanie typów