Learn Rust in a Month of Lunches....1
brief contents....8
contents....9
foreword....17
preface....19
acknowledgments....21
about this book....23
Who should read this book....23
How this book is organized: A road map....24
About the code....25
liveBook discussion forum....25
about the author....26
1 Some basics....27
1.1 Introducing Rust....28
1.1.1 A pep talk....28
1.1.2 Rust is like a critical spouse....29
1.2 Comments....32
1.3 Primitive types: Integers, characters, and strings....33
1.4 Type inference....40
1.5 Floats....41
1.6 “Hello, World!” and printing....42
1.7 Declaring variables and code blocks....46
1.8 Display and Debug....48
1.9 Smallest and largest numbers....49
1.10 Mutability (changing)....50
1.11 Shadowing....51
Summary....53
2 Memory, variables, and ownership....54
2.1 The stack, the heap, pointers, and references....55
2.2 Strings....56
2.3 const and static....59
2.4 More on references....60
2.5 Mutable references....61
2.5.1 Rust’s reference rules....62
2.5.2 Situation 1: Only one mutable reference....62
2.5.3 Situation 2: Only immutable references....63
2.5.4 Situation 3: The problem situation....63
2.6 Shadowing again....64
2.7 Giving references to functions....64
2.8 Copy types....68
2.9 Variables without values....71
2.10 More about printing....71
Summary....77
3 More complex types....78
3.1 Collection types....78
3.1.1 Arrays....79
3.1.2 Vectors....81
3.1.3 Tuples....84
3.2 Control flow....86
3.2.1 Basic control flow....86
3.2.2 Match statements....87
3.2.3 Loops....92
Summary....96
4 Building your own types....97
4.1 A quick overview of structs and enums....97
4.1.1 Structs....98
4.1.2 Enums....101
4.1.3 Casting enums into integers....104
4.1.4 Enums to use multiple types....105
4.1.5 Implementing structs and enums....107
4.2 Destructuring....109
4.3 References and the dot operator....113
Summary....114
5 Generics, option, and result....116
5.1 Generics....116
5.2 Option and Result....121
5.2.1 Option....122
5.2.2 Result....125
5.2.3 Some other ways to do pattern matching....128
Summary....131
6 More collections, more error handling....132
6.1 Other collections....132
6.1.1 HashMap (and BTreeMap)....133
6.1.2 HashSet and BTreeSet....139
6.1.3 BinaryHeap....141
6.1.4 VecDeque....142
6.2 The ? operator....144
6.3 When panic and unwrap are good....148
Summary....152
7 Traits: Making different types do the same thing....154
7.1 Traits: The basics....154
7.1.1 All you need are the method signatures....157
7.1.2 More complex examples....161
7.1.3 Traits as bounds....166
7.1.4 Traits are like qualifications....168
7.2 The From trait....171
7.3 The orphan rule....173
7.4 Getting around the orphan rule with newtypes....173
7.5 Taking a String and a &str in a function....175
Summary....177
8 Iterators and closures....178
Iterators and closures....178
8.1 Chaining methods....179
8.2 Iterators....180
8.3 Closures and closures inside iterators....187
8.3.1 Closures inside of methods....188
8.3.2 Closures: Lazy and fast....190
8.3.3 |_| in a closure....193
Summary....194
9 Iterators and closures again!....195
9.1 Helpful methods for closures and iterators....196
9.1.1 Mapping and filtering....196
9.1.2 Some more iterator and related methods....200
9.1.3 Checking and finding items inside iterators....203
9.1.4 Cycling, zipping, folding, and more....206
9.2 The dbg! macro and .inspect....211
Summary....214
10 Lifetimes and interior mutability....215
10.1 Types of &str....215
10.2 Lifetime annotations....216
10.2.1 Lifetimes in functions....217
10.2.2 Lifetime annotations in types....218
10.2.3 The anonymous lifetime....221
10.3 Interior mutability....225
10.3.1 Cell....226
10.3.2 RefCell....227
10.3.3 Mutex....230
10.3.4 RwLock....232
Summary....234
11 Multiple threads and a lot more....235
11.1 Importing and renaming inside a function....236
11.2 The todo! macro....238
11.3 Type aliases....240
11.4 Cow....241
11.5 Rc....245
11.5.1 Why Rc exists....245
11.5.2 Using Rc in practice....246
11.5.3 Avoiding lifetime annotations with Rc....249
11.6 Multiple threads....251
11.6.1 Spawning threads....251
11.6.2 Using JoinHandles to wait for threads to finish....253
11.6.3 Types of closures....255
11.6.4 Using the move keyword....256
Summary....258
12 More on closures, generics, and threads....259
12.1 Closures as arguments....260
12.1.1 Some simple closures....263
12.1.2 The relationship between FnOnce, FnMut, and Fn....265
12.1.3 Closures are all unique....266
12.1.4 A closure example....268
12.2 impl Trait....270
12.2.1 Regular generics compared to impl Trait....270
12.2.2 Returning closures with impl Trait....272
12.3 Arc....275
12.4 Scoped threads....279
12.5 Channels....282
12.5.1 Channel basics....282
12.5.2 Implementing a channel....283
Summary....286
13 Box and Rust documentation....287
13.1 Reading Rust documentation....288
13.1.1 assert_eq!....288
13.1.2 Searching....289
13.1.3 The [src] button....290
13.1.4 Information on traits....291
13.1.5 Attributes....291
13.2 Box....297
13.2.1 Some Box basics....297
13.2.2 Putting a Box around traits....299
13.2.3 Using a Box to handle multiple error types....302
13.2.4 Downcasting to a concrete type....305
Summary....307
14 Testing and building your code from tests....308
14.1 Crates and modules....309
14.1.1 Module basics....309
14.1.2 More on how the pub keyword works....311
14.1.3 Modules inside modules....312
14.2 Testing....315
14.2.1 Just add #[test], and now it’s a test....315
14.2.2 What happens when tests fail....316
14.2.3 Writing multiple tests....318
14.3 Test-driven development....319
14.3.1 Building a calculator: Starting with the tests....320
14.3.2 Putting the calculator together....322
Summary....328
15 Default, the builder pattern, and Deref....329
15.1 Implementing Default....329
15.2 The builder pattern....332
15.2.1 Writing builder methods....333
15.2.2 Adding a final check to the builder pattern....335
15.2.3 Making the builder pattern more rigorous....338
15.3 Deref and DerefMut....340
15.3.1 Deref basics....340
15.3.2 Implementing Deref....342
15.3.3 Implementing DerefMut....344
15.3.4 Using Deref the wrong way....345
Summary....348
16 Const, “unsafe” Rust, and external crates....349
16.1 Const generics....350
16.2 Const functions....352
16.3 Mutable statics....354
16.4 Unsafe Rust....355
16.4.1 Overview of unsafe Rust....355
16.4.2 Using static mut in unsafe Rust....357
16.4.3 Rust’s most famous unsafe method....358
16.4.4 Methods ending in _unchecked....360
16.5 Introducing external crates....361
16.5.1 Crates and Cargo.toml....361
16.5.2 Using the rand crate....362
16.5.3 Rolling some dice with rand....363
Summary....365
17 Rust’s most popular crates....367
17.1 serde....368
17.2 Time in the standard library....370
17.3 chrono....375
17.3.1 Checking the code inside external crates....376
17.3.2 Back to chrono....377
17.4 Rayon....380
17.5 Anyhow and thiserror....383
17.5.1 Anyhow....383
17.5.2 thiserror....386
17.6 Blanket trait implementations....388
17.7 lazy_static and once_cell....392
17.7.1 Lazy static: Lazily evaluated statics....392
17.7.2 OnceCell: A cell to only write to once....395
Summary....397
18 Rust on your computer....399
18.1 Cargo....400
18.1.1 Why everyone uses Cargo....400
18.1.2 Using Cargo and what Rust does while it compiles....402
18.2 Working with user input....406
18.2.1 User input through stdin....406
18.2.2 Accessing command-line arguments....408
18.2.3 Accessing environment variables....411
18.3 Using files....414
18.3.1 Creating files....414
18.3.2 Opening existing files....415
18.3.3 Using OpenOptions to work with files....415
18.4 cargo doc....418
Summary....420
19 More crates and async Rust....422
19.1 The reqwest crate....422
19.2 Feature flags....425
19.3 Async Rust....428
19.3.1 Async basics....428
19.3.2 Checking whether a Future is ready....429
19.3.3 Using an async run time....431
19.3.4 Some other details about async Rust....433
Summary....436
20 A tour of the standard library....438
20.1 Arrays....439
20.1.1 Arrays now implement Iterator....439
20.1.2 Destructuring and mapping arrays....440
20.1.3 Using from_fn to make arrays....441
20.2 char....443
20.3 Integers....445
20.3.1 Checked operations....445
20.3.2 The Add trait and other similar traits....446
20.4 Floats....449
20.5 Associated items and associated constants....450
20.5.1 Associated functions....450
20.5.2 Associated types....451
20.5.3 Associated consts....453
20.6 bool....454
20.7 Vec....456
20.8 String....458
20.9 OsString and CString....460
Summary....461
21 Continuing the tour....463
21.1 std::mem....463
21.2 Setting panic hooks....468
21.3 Viewing backtraces....473
21.4 The standard library prelude....476
21.5 Other macros....477
21.5.1 unreachable!....478
21.5.2 column!, line!, file!, and module_path!....480
21.5.3 thread_local!....482
21.5.4 cfg!....484
Summary....486
22 Writing your own macros....487
22.1 Why macros exist....488
22.2 Writing basic macros....489
22.3 Reading macros from the standard library....496
22.4 Using macros to keep your code clean....501
Summary....505
23 Unfinished projects: Projects for you to finish....506
23.1 Setup for the last two chapters....507
23.2 Typing tutor....507
23.2.1 Setup and first code....507
23.2.2 Developing the code....508
23.2.3 Further development and cleanup....510
23.2.4 Over to you....512
23.3 Wikipedia article summary searcher....513
23.3.1 Setup and first code....513
23.3.2 Developing the code....514
23.3.3 Further development and cleanup....516
23.3.4 Over to you....518
23.4 Terminal stopwatch and clock....518
23.4.1 Setup and first code....518
23.4.2 Developing the code....521
23.4.3 Further development and cleanup....525
23.4.4 Over to you....527
Summary....528
24 Unfinished projects, continued....529
24.1 Web server word-guessing game....529
24.1.1 Setup and first code....530
24.1.2 Developing the code....532
24.1.3 Further development and cleanup....535
24.1.4 Over to you....538
24.2 Laser pointer....538
24.2.1 Setup and first code....538
24.2.2 Developing the code....540
24.2.3 Further development and cleanup....543
24.2.4 Over to you....546
24.3 Directory and file navigator....546
24.3.1 Setup and first code....547
24.3.2 Developing the code....548
24.3.3 Further development and cleanup....550
24.3.4 Over to you....552
Summary....553
index....555
Symbols....555
A....555
B....556
C....556
D....557
E....558
F....558
G....559
H....559
I....559
J....560
K....560
L....560
M....560
N....561
O....561
P....561
Q....562
R....562
S....563
T....564
U....565
V....566
W....566
Y....566
Z....566
Learn Rust in a Month of Lunches -back....569
Learn Rust in a Month of Lunches teaches you to write super fast and super safe Rust code through lessons you can fit in your lunch break. Crystal-clear explanations and focused, relevant examples make it accessible to anyone—even if you’re learning Rust as your first programming language.
Learn Rust in a Month of Lunches is full of 24 easy-to-digest lessons that ease you into real Rust programming. You’ll learn essential Rust skills you can use for everything from system programming, to web applications, and games. By the time you’re done learning, you’ll know exactly what makes Rust unique—and be one of the thousands of developers who say it’s their best loved language!
Learn how to create fast powerful programs in Rust in just 24 short lessons! Rust gives you modern features like a top-notch compiler, a rich ecosystem of pre-built libraries, and the same low-level performance you get with a language like C, but without the awkward syntax, complex memory management, and code safety concerns. This book guides you step by step from your first line of code.
Learn Rust in a Month of Lunches breaks down the Rust language into concise hands-on lessons designed to be completed in an hour or less. The examples are fun and easy to follow, so you’ll quickly progress from zero Rust knowledge to handling async and writing your own macros. You won’t even need to install Rust—the book’s code samples run in the browser-based Rust Playground. There’s no easier way to get started!
No previous experience with Rust required.