Programming with Rust
Publisher,Addison-Wesley Professional
Publication Date,
Format, Paperback
Weight, 698 g
No. of Pages, 400
Rust's exciting innovations have made it the most loved programming language in Stack Overflow's influential survey for five straight years—but its steep learning curve has made many other developers reluctant to dive in. Now, with a growing commitment to Rust from many of the world's leading development organizations, it's the perfect time to start—especially now that there's an up-to-date, accessible, example-rich book to guide you. Long-time enterprise developer Donis Marshall has made Rust easier to understand than ever, with a guide expertly organized into short, bite-sized chapters and supported with focused video tutorials that bring you up-to-speed fast.
Writing for developers at all levels, Marshall starts with the absolute basics, and thoroughly demystifies the Rust technical advances that make it so attractive for next-generation development. Everything's here, from types and assignments to ownership, lifetimes, traits, and crates. Marshall even offers indispensable expert advice for unit testing, handling unsafe code, interoperating with legacy code bases, and using Rust's increasingly robust tools.
More than just a new language, Rust represents a philosophical shift in how you code. With Programming in Rust, you'll master the techniques and the mindset, make Rust a key tool in your arsenal, and access one of the industry's fastest-growing areas of opportunity.
Chapter 1: Introduction to Rust1
Introduction 1
Features 4
Rust Terminology 6
Tools 7
Summary 8
Chapter 2: Getting Started9
Preliminaries 9
Rust and Windows 10
Installing Rust 10
Advanced Rustup 11
“Hello, World” 11
Compile and Run 12
Cargo 13
Library 15
Comments 17
Published Crates 20
Main Function 21
Command-Line Arguments 22
Summary 23
Chapter 3: Variables25
Terminology 25
Variables 26
Primitives 26
Integer Types 27
Overflow 28
Notations 29
Floating Point Types 30
Floating Point Constants 30
Infinity 31
NaN 31
Numeric Ranges 32
Casting 33
Boolean Types 34
Char 34
Pointers 35
Operators 37
Summary 40
Chapter 4: Strings41
Str 41
String 42
Length 43
Extending a String 44
Capacity 45
Accessing a String Value 46
String Characters 48
Deref Coercion 48
Formatted String 49
Helpful Functions 49
Summary 52
Chapter 5: Console53
Print 53
Positional Arguments 54
Variable Arguments 55
Named Arguments 55
Padding, Alignment, and Precision 56
Base 58
Developer Facing 59
Write! Macro 60
Display Trait 60
Debug Trait 62
Format! Macro 63
Console Read and Write 63
Summary 64
Chapter 6: Control Flow65
The if Expression 65
The while Expression 68
The for Expression 70
The loop Expression 73
The Iterator Trait 76
Summary 78
Chapter 7: Collections79
Arrays 79
Vectors 86
HashMap 91
Summary 95
Chapter 8: Ownership97
Stack and Heap Memory 98
Shallow versus Deep Copy 98
Car Analogy 99
Move Semantics 99
Borrow 101
Copy Semantics 101
Clone Trait 102
Copy Trait 103
Clone Trait 104
Summary 105
Chapter 9: Lifetimes107
Introduction to Lifetimes 108
Function Headers and Lifetimes 109
Lifetime Annotation 111
Lifetime Elision 112
Complex Lifetimes 113
Sharing a Lifetime 114
Static Lifetimes 115
Structs and Lifetimes 116
Methods and Lifetimes 116
Subtyping Lifetimes 118
Anonymous Lifetimes 120
Generics and Lifetimes 121
Summary 121
Chapter 10: References123
Declaration 124
Borrowing 124
Dereferencing 125
Comparing References 126
Reference Notation 127
Reference to Reference 128
Mutability 129
Limits to Multiple Borrowers 130
Summary 131
Chapter 11: Functions133
Function Definition 133
Parameters 134
Function Return 136
Const Functions 138
Nested Functions 139
Function Pointers 140
Function Aliases 142
Summary 143
Chapter 12: Error Handling145
Handling Error Handling 145
The Result Enum 146
The Option Enum 147
Panics 148
Unwrapping 154
Match Pattern for Result and Option 156
Map 158
Rich Errors 160
Custom Errors 161
Summary 163
Chapter 13: Structures165
Alternate Initialization 167
Move Semantics 169
Mutability 170
Methods 170
Self 172
Associated Functions 173
Impl Blocks 174
Operator Overloading 175
Tuple Struct 179
Summary 180
Chapter 14: Generics181
Generic Functions 182
Bounds 186
The where Clause 188
Structs 190
Associated Functions 194
Enums 195
Generic Traits 197
Explicit Specialization 200
Summary 205
Chapter 15: Patterns207
Let Statement 207
Wildcards 208
Complex Patterns 209
Ownership 210
Irrefutable 212
Ranges 213
Multiple Patterns 214
Control Flow 215
Structs 216
Functions 219
Match Expressions 220
Match Guards 224
Summary 227
Chapter 16: Closures229
“Hello, World” 229
Closure Syntax 230
Closed Over 231
Closures as Function Arguments 233
Closures as Function Return Values 234
Implementation of Closures 235
Matrix Example 242
Summary 244
Chapter 17: Traits245
Trait Definition 245
Default Functions 248
Marker Trait 249
Associated Functions 249
Associated Types 251
Extension Methods 253
Fully Qualified Syntax 254
Supertraits 257
Static Dispatch 260
Dynamic Dispatch 262
Enums and Traits 263
Summary 265
Chapter 18: Threads1 267
Synchronous Function Calls 268
Threads 269
The Thread Type 273
Processor Time 274
Builder 275
Communicating Sequential Process 276
Asynchronous Channel 277
Synchronous Channel 279
Rendezvous Channel 280
The try Methods 281
Store Example 286
Summary 290
Chapter 19: Threads2 291
Mutex 291
Nonscoped Mutex 294
Mutex Poisoning 296
Reader-Writer Lock 297
Condition Variables 299
Atomic Operations 301
Summary 305
Chapter 20: Memory307
Stacks 307
Static Values 309
The Heap 310
Interior Mutability 312
RefCell 316
OnceCell 318
Summary 319
Chapter 21: Macros321
Tokens 322
Declarative Macros 323
Repetition 325
Multiple Macro Matchers 327
Procedural Macros 328
Derive Macros 329
Attribute Macros 332
Function-Like Macros 334
Summary 335
Chapter 22: Interoperability337
Foreign Function Interface 337
Basic Example 339
Libc Crate 341
Structs 342
Bindgen 346
C Calling Rust Functions 347
Cbindgen 348
Summary 351
Chapter 23: Modules353
Module Items 354
Module Files 358
The path Attribute 360
Functions and Modules 361
The crate, super, and self Keywords 362
Legacy Model 363
Summary 365
Index367