Preface |
|
xv | |
1 Systems Programmers Can Have Nice Things |
|
1 | (4) |
|
Rust Shoulders the Load for You |
|
|
2 | (1) |
|
Parallel Programming Is Tamed |
|
|
3 | (1) |
|
And Yet Rust Is Still Fast |
|
|
4 | (1) |
|
Rust Makes Collaboration Easier |
|
|
4 | (1) |
2 A Tour of Rust |
|
5 | (44) |
|
|
6 | (2) |
|
|
8 | (2) |
|
Writing and Running Unit Tests |
|
|
10 | (1) |
|
Handling Command-Line Arguments |
|
|
11 | (4) |
|
|
15 | (7) |
|
|
22 | (20) |
|
What the Mandelbrot Set Actually Is |
|
|
23 | (5) |
|
Parsing Pair Command-Line Arguments |
|
|
28 | (2) |
|
Mapping from Pixels to Complex Numbers |
|
|
30 | (2) |
|
|
32 | (1) |
|
|
33 | (2) |
|
A Concurrent Mandelbrot Program |
|
|
35 | (5) |
|
Running the Mandelbrot Plotter |
|
|
40 | (1) |
|
|
41 | (1) |
|
Filesystems and Command-Line Tools |
|
|
42 | (7) |
|
The Command-Line Interface |
|
|
43 | (2) |
|
Reading and Writing Files |
|
|
45 | (1) |
|
|
46 | (3) |
3 Fundamental Types |
|
49 | (30) |
|
Fixed-Width Numeric Types |
|
|
52 | (9) |
|
|
53 | (3) |
|
Checked, Wrapping, Saturating, and Overflowing Arithmetic |
|
|
56 | (2) |
|
|
58 | (3) |
|
|
61 | (1) |
|
|
61 | (2) |
|
|
63 | (2) |
|
|
65 | (2) |
|
|
65 | (1) |
|
|
66 | (1) |
|
|
66 | (1) |
|
Arrays, Vectors, and Slices |
|
|
67 | (6) |
|
|
67 | (1) |
|
|
68 | (3) |
|
|
71 | (2) |
|
|
73 | (5) |
|
|
73 | (1) |
|
|
74 | (1) |
|
|
74 | (2) |
|
|
76 | (1) |
|
|
77 | (1) |
|
|
77 | (1) |
|
|
78 | (1) |
|
|
78 | (1) |
4 Ownership and Moves |
|
79 | (22) |
|
|
81 | (4) |
|
|
85 | (9) |
|
More Operations That Move |
|
|
90 | (1) |
|
|
91 | (1) |
|
Moves and Indexed Content |
|
|
92 | (2) |
|
Copy Types: The Exception to Moves |
|
|
94 | (4) |
|
Rc and Arc: Shared Ownership |
|
|
98 | (3) |
5 References |
|
101 | (32) |
|
|
102 | (3) |
|
|
105 | (5) |
|
Rust References Versus C++ References |
|
|
105 | (2) |
|
|
107 | (1) |
|
|
107 | (1) |
|
|
108 | (1) |
|
References Are Never Null |
|
|
109 | (1) |
|
Borrowing References to Arbitrary Expressions |
|
|
109 | (1) |
|
References to Slices and Trait Objects |
|
|
110 | (1) |
|
|
110 | (13) |
|
Borrowing a Local Variable |
|
|
110 | (3) |
|
Receiving References as Function Arguments |
|
|
113 | (2) |
|
Passing References to Functions |
|
|
115 | (1) |
|
|
116 | (1) |
|
Structs Containing References |
|
|
117 | (3) |
|
Distinct Lifetime Parameters |
|
|
120 | (1) |
|
Omitting Lifetime Parameters |
|
|
121 | (2) |
|
|
123 | (7) |
|
Taking Arms Against a Sea of Objects |
|
|
130 | (3) |
6 Expressions |
|
133 | (24) |
|
|
133 | (1) |
|
Precedence and Associativity |
|
|
134 | (3) |
|
|
137 | (1) |
|
|
138 | (2) |
|
|
140 | (2) |
|
|
142 | (1) |
|
|
142 | (2) |
|
|
144 | (1) |
|
|
145 | (1) |
|
|
146 | (2) |
|
Function and Method Calls |
|
|
148 | (1) |
|
|
149 | (2) |
|
|
151 | (1) |
|
Arithmetic, Bitwise, Comparison, and Logical Operators |
|
|
151 | (1) |
|
|
152 | (1) |
|
|
153 | (1) |
|
|
154 | (1) |
|
|
154 | (3) |
7 Error Handling |
|
157 | (16) |
|
|
157 | (3) |
|
|
158 | (1) |
|
|
159 | (1) |
|
|
160 | (13) |
|
|
160 | (2) |
|
|
162 | (1) |
|
|
163 | (1) |
|
|
164 | (2) |
|
Working with Multiple Error Types |
|
|
166 | (2) |
|
Dealing with Errors That "Can't Happen" |
|
|
168 | (1) |
|
|
169 | (1) |
|
Handling Errors in main() |
|
|
169 | (2) |
|
Declaring a Custom Error Type |
|
|
171 | (1) |
|
|
172 | (1) |
8 Crates and Modules |
|
173 | (36) |
|
|
173 | (5) |
|
|
176 | (1) |
|
|
177 | (1) |
|
|
178 | (10) |
|
|
179 | (1) |
|
Modules in Separate Files |
|
|
180 | (3) |
|
|
183 | (3) |
|
|
186 | (1) |
|
Making use Declarations pub |
|
|
186 | (1) |
|
|
186 | (1) |
|
|
187 | (1) |
|
Turning a Program into a Library |
|
|
188 | (1) |
|
|
189 | (2) |
|
|
191 | (2) |
|
|
193 | (9) |
|
|
196 | (1) |
|
|
197 | (2) |
|
|
199 | (3) |
|
|
202 | (3) |
|
|
202 | (2) |
|
|
204 | (1) |
|
Publishing Crates to crates.io |
|
|
205 | (2) |
|
|
207 | (1) |
|
|
208 | (1) |
9 Structs |
|
209 | (20) |
|
|
209 | (3) |
|
|
212 | (1) |
|
|
213 | (1) |
|
|
213 | (1) |
|
Defining Methods with impl |
|
|
214 | (6) |
|
Passing Self as a Box, Rc, or Arc |
|
|
217 | (2) |
|
Type-Associated Functions |
|
|
219 | (1) |
|
|
220 | (1) |
|
|
221 | (2) |
|
Structs with Lifetime Parameters |
|
|
223 | (1) |
|
Deriving Common Traits for Struct Types |
|
|
224 | (1) |
|
|
225 | (4) |
10 Enums and Patterns |
|
229 | (24) |
|
|
230 | (9) |
|
|
232 | (1) |
|
|
233 | (1) |
|
Rich Data Structures Using Enums |
|
|
234 | (2) |
|
|
236 | (3) |
|
|
239 | (13) |
|
Literals, Variables, and Wildcards in Patterns |
|
|
242 | (1) |
|
Tuple and Struct Patterns |
|
|
243 | (1) |
|
|
244 | (1) |
|
|
245 | (2) |
|
|
247 | (1) |
|
Matching Multiple Possibilities |
|
|
248 | (1) |
|
|
248 | (1) |
|
Where Patterns Are Allowed |
|
|
249 | (1) |
|
|
250 | (2) |
|
|
252 | (1) |
11 Traits and Generics |
|
253 | (32) |
|
|
255 | (9) |
|
|
256 | (2) |
|
Generic Functions and Type Parameters |
|
|
258 | (3) |
|
|
261 | (3) |
|
Defining and Implementing Traits |
|
|
264 | (7) |
|
|
265 | (1) |
|
Traits and Other People's Types |
|
|
266 | (2) |
|
|
268 | (1) |
|
|
269 | (1) |
|
Type-Associated Functions |
|
|
270 | (1) |
|
Fully Qualified Method Calls |
|
|
271 | (2) |
|
Traits That Define Relationships Between Types |
|
|
273 | (8) |
|
Associated Types (or How Iterators Work) |
|
|
273 | (4) |
|
Generic Traits (or How Operator Overloading Works) |
|
|
277 | (1) |
|
|
278 | (2) |
|
|
280 | (1) |
|
Reverse-Engineering Bounds |
|
|
281 | (3) |
|
|
284 | (1) |
12 Operator Overloading |
|
285 | (16) |
|
Arithmetic and Bitwise Operators |
|
|
286 | (6) |
|
|
289 | (1) |
|
|
290 | (1) |
|
Compound Assignment Operators |
|
|
291 | (1) |
|
|
292 | (3) |
|
|
295 | (3) |
|
|
298 | (2) |
|
|
300 | (1) |
13 Utility Traits |
|
301 | (24) |
|
|
302 | (3) |
|
|
305 | (3) |
|
|
308 | (1) |
|
|
309 | (1) |
|
|
310 | (3) |
|
|
313 | (2) |
|
|
315 | (1) |
|
|
316 | (2) |
|
|
318 | (3) |
|
|
321 | (1) |
|
|
322 | (1) |
|
Borrow and ToOwned at Work: The Humble Cow |
|
|
323 | (2) |
14 Closures |
|
325 | (20) |
|
|
327 | (3) |
|
|
328 | (1) |
|
|
328 | (2) |
|
Function and Closure Types |
|
|
330 | (2) |
|
|
332 | (1) |
|
|
333 | (6) |
|
|
334 | (1) |
|
|
334 | (2) |
|
|
336 | (2) |
|
Copy and Clone for Closures |
|
|
338 | (1) |
|
|
339 | (4) |
|
Using Closures Effectively |
|
|
343 | (2) |
15 Iterators |
|
345 | (46) |
|
The Iterator and Intolterator Traits |
|
|
347 | (1) |
|
|
348 | (8) |
|
iter and iter_mut Methods |
|
|
348 | (1) |
|
Intolterator Implementations |
|
|
349 | (2) |
|
|
351 | (2) |
|
|
353 | (1) |
|
|
354 | (2) |
|
|
356 | (17) |
|
|
356 | (3) |
|
|
359 | (2) |
|
|
361 | (2) |
|
|
363 | (1) |
|
|
363 | (1) |
|
|
364 | (1) |
|
|
365 | (1) |
|
Reversible Iterators and rev |
|
|
366 | (1) |
|
|
367 | (1) |
|
|
368 | (1) |
|
|
369 | (1) |
|
|
370 | (1) |
|
|
370 | (1) |
|
|
371 | (1) |
|
|
372 | (1) |
|
|
373 | (12) |
|
Simple Accumulation: count, sum, product |
|
|
373 | (1) |
|
|
374 | (1) |
|
|
374 | (1) |
|
|
375 | (1) |
|
|
375 | (1) |
|
|
376 | (1) |
|
position, rposition, and ExactSizelterator |
|
|
376 | (1) |
|
|
377 | (1) |
|
|
378 | (1) |
|
|
379 | (1) |
|
|
380 | (1) |
|
find, rfind, and find_map |
|
|
380 | (1) |
|
Building Collections: collect and Fromlterator |
|
|
381 | (2) |
|
|
383 | (1) |
|
|
383 | (1) |
|
for_each and try_for_each |
|
|
384 | (1) |
|
Implementing Your Own Iterators |
|
|
385 | (6) |
16 Collections |
|
391 | (36) |
|
|
392 | (1) |
|
|
393 | (15) |
|
|
394 | (2) |
|
|
396 | (1) |
|
Growing and Shrinking Vectors |
|
|
396 | (4) |
|
|
400 | (1) |
|
|
400 | (3) |
|
|
403 | (1) |
|
|
404 | (2) |
|
|
406 | (1) |
|
|
406 | (1) |
|
Rust Rules Out Invalidation Errors |
|
|
407 | (1) |
|
|
408 | (2) |
|
|
410 | (1) |
|
HashMap K, V and BTreeMap K, V |
|
|
411 | (6) |
|
|
415 | (2) |
|
|
417 | (5) |
|
|
418 | (1) |
|
|
419 | (1) |
|
When Equal Values Are Different |
|
|
420 | (1) |
|
|
420 | (2) |
|
|
422 | (1) |
|
Using a Custom Hashing Algorithm |
|
|
423 | (1) |
|
Beyond the Standard Collections |
|
|
424 | (3) |
17 Strings and Text |
|
427 | (44) |
|
|
428 | (2) |
|
ASCII, Latin-1, and Unicode |
|
|
428 | (1) |
|
|
428 | (2) |
|
|
430 | (1) |
|
|
430 | (4) |
|
|
431 | (1) |
|
|
432 | (1) |
|
Case Conversion for Characters |
|
|
433 | (1) |
|
Conversions to and from Integers |
|
|
434 | (1) |
|
|
434 | (17) |
|
|
435 | (1) |
|
|
436 | (1) |
|
Appending and Inserting Text |
|
|
437 | (1) |
|
Removing and Replacing Text |
|
|
438 | (1) |
|
Conventions for Searching and Iterating |
|
|
439 | (1) |
|
Patterns for Searching Text |
|
|
440 | (1) |
|
|
441 | (1) |
|
|
442 | (2) |
|
|
444 | (1) |
|
Case Conversion for Strings |
|
|
445 | (1) |
|
Parsing Other Types from Strings |
|
|
445 | (1) |
|
Converting Other Types to Strings |
|
|
446 | (1) |
|
Borrowing as Other Text-Like Types |
|
|
447 | (1) |
|
|
447 | (1) |
|
Producing Text from UTF-8 Data |
|
|
448 | (1) |
|
|
449 | (2) |
|
Strings as Generic Collections |
|
|
451 | (1) |
|
|
451 | (12) |
|
|
453 | (1) |
|
|
454 | (2) |
|
|
456 | (1) |
|
Formatting Values for Debugging |
|
|
456 | (2) |
|
Formatting Pointers for Debugging |
|
|
458 | (1) |
|
Referring to Arguments by Index or Name |
|
|
458 | (1) |
|
Dynamic Widths and Precisions |
|
|
459 | (1) |
|
Formatting Your Own Types |
|
|
460 | (2) |
|
Using the Formatting Language in Your Own Code |
|
|
462 | (1) |
|
|
463 | (3) |
|
|
464 | (1) |
|
Building Regex Values Lazily |
|
|
465 | (1) |
|
|
466 | (5) |
|
|
467 | (1) |
|
The unicode-normalization Crate |
|
|
468 | (3) |
18 Input and Output |
|
471 | (26) |
|
|
472 | (14) |
|
|
473 | (2) |
|
|
475 | (2) |
|
|
477 | (2) |
|
|
479 | (1) |
|
|
480 | (1) |
|
|
481 | (1) |
|
|
482 | (1) |
|
Other Reader and Writer Types |
|
|
483 | (2) |
|
Binary Data, Compression, and Serialization |
|
|
485 | (1) |
|
|
486 | (8) |
|
|
486 | (2) |
|
|
488 | (2) |
|
Filesystem Access Functions |
|
|
490 | (1) |
|
|
491 | (2) |
|
Platform-Specific Features |
|
|
493 | (1) |
|
|
494 | (3) |
19 Concurrency |
|
497 | (42) |
|
|
499 | (11) |
|
|
500 | (2) |
|
Error Handling Across Threads |
|
|
502 | (1) |
|
Sharing Immutable Data Across Threads |
|
|
503 | (2) |
|
|
505 | (3) |
|
Revisiting the Mandelbrot Set |
|
|
508 | (2) |
|
|
510 | (13) |
|
|
511 | (3) |
|
|
514 | (1) |
|
|
515 | (2) |
|
Channel Features and Performance |
|
|
517 | (2) |
|
Thread Safety: Send and Sync |
|
|
519 | (2) |
|
Piping Almost Any Iterator to a Channel |
|
|
521 | (2) |
|
|
523 | (1) |
|
|
523 | (15) |
|
|
524 | (2) |
|
|
526 | (1) |
|
|
527 | (1) |
|
Why Mutexes Are Not Always a Good Idea |
|
|
528 | (1) |
|
|
529 | (1) |
|
|
529 | (1) |
|
Multiconsumer Channels Using Mutexes |
|
|
530 | (1) |
|
Read/Write Locks (RwLock T) |
|
|
531 | (1) |
|
Condition Variables (Condvar) |
|
|
532 | (1) |
|
|
533 | (2) |
|
|
535 | (3) |
|
What Hacking Concurrent Code in Rust Is Like |
|
|
538 | (1) |
20 Asynchronous Programming |
|
539 | (60) |
|
From Synchronous to Asynchronous |
|
|
541 | (23) |
|
|
542 | (2) |
|
Async Functions and Await Expressions |
|
|
544 | (3) |
|
Calling Async Functions from Synchronous Code: block_on |
|
|
547 | (3) |
|
|
550 | (4) |
|
|
554 | (2) |
|
Building Async Functions from Async Blocks |
|
|
556 | (1) |
|
Spawning Async Tasks on a Thread Pool |
|
|
557 | (1) |
|
But Does Your Future Implement Send? |
|
|
558 | (3) |
|
Long Running Computations: yield_now and spawn_blocking |
|
|
561 | (1) |
|
Comparing Asynchronous Designs |
|
|
562 | (1) |
|
A Real Asynchronous HTTP Client |
|
|
563 | (1) |
|
An Asynchronous Client and Server |
|
|
564 | (18) |
|
|
566 | (1) |
|
|
567 | (1) |
|
Taking User Input: Asynchronous Streams |
|
|
568 | (2) |
|
|
570 | (1) |
|
Receiving Packets: More Asynchronous Streams |
|
|
571 | (2) |
|
The Client's Main Function |
|
|
573 | (1) |
|
The Server's Main Function |
|
|
574 | (1) |
|
Handling Chat Connections: Async Mutexes |
|
|
575 | (3) |
|
The Group Table: Synchronous Mutexes |
|
|
578 | (1) |
|
Chat Groups: tokio's Broadcast Channels |
|
|
579 | (3) |
|
Primitive Futures and Executors: When Is a Future Worth Polling Again? |
|
|
582 | (6) |
|
Invoking Wakers: spawn_blocking |
|
|
584 | (2) |
|
|
586 | (2) |
|
|
588 | (7) |
|
The Two Life Stages of a Future |
|
|
588 | (4) |
|
|
592 | (2) |
|
|
594 | (1) |
|
When Is Asynchronous Code Helpful? |
|
|
595 | (4) |
21 Macros |
|
599 | (26) |
|
|
600 | (7) |
|
Basics of Macro Expansion |
|
|
601 | (2) |
|
|
603 | (2) |
|
|
605 | (2) |
|
|
607 | (2) |
|
|
609 | (1) |
|
|
610 | (12) |
|
|
611 | (4) |
|
|
615 | (1) |
|
|
615 | (2) |
|
|
617 | (3) |
|
Importing and Exporting Macros |
|
|
620 | (2) |
|
Avoiding Syntax Errors During Matching |
|
|
622 | (1) |
|
|
623 | (2) |
22 Unsafe Code |
|
625 | (38) |
|
|
626 | (2) |
|
|
628 | (1) |
|
Example: An Efficient ASCII String Type |
|
|
629 | (2) |
|
|
631 | (2) |
|
Unsafe Block or Unsafe Function? |
|
|
633 | (1) |
|
|
634 | (2) |
|
|
636 | (2) |
|
|
638 | (20) |
|
Dereferencing Raw Pointers Safely |
|
|
641 | (1) |
|
|
642 | (2) |
|
|
644 | (1) |
|
Type Sizes and Alignments |
|
|
645 | (1) |
|
|
645 | (2) |
|
Moving into and out of Memory |
|
|
647 | (4) |
|
|
651 | (6) |
|
Panic Safety in Unsafe Code |
|
|
657 | (1) |
|
Reinterpreting Memory with Unions |
|
|
658 | (3) |
|
|
661 | (1) |
|
|
661 | (2) |
23 Foreign Functions |
|
663 | |
|
Finding Common Data Representations |
|
|
664 | (4) |
|
Declaring Foreign Functions and Variables |
|
|
668 | (1) |
|
Using Functions from Libraries |
|
|
669 | (4) |
|
A Raw Interface to libgit2 |
|
|
673 | (6) |
|
A Safe Interface to libgit2 |
|
|
679 | (11) |
|
|
690 | |
Index |
|
69 | |