Table of Contents

Open all
Close all
Preface
19
The Purpose of This Book
19
The Structure of This Book
19
Target Audience
22
Acknowledgments
22
PART I Basic Programming with Rust
25
1 Introduction
27
1.1 Installing Rust and Its Web-Based Environment
27
1.1.1 Installing Rust
27
1.1.2 Rust’s Web-Based Compiler
29
1.2 Running and Compiling Your First Program
30
1.3 Visual Studio Code Settings
32
1.4 Making the Most of This Book
34
1.5 Summary
36
2 Variables, Data Types, and Functions
39
2.1 Variables
39
2.1.1 Definition
39
2.1.2 Mutability of Variables
40
2.1.3 Scope of Variables
41
2.1.4 Shadowing
41
2.1.5 Constants
43
2.1.6 Statics
43
2.1.7 Unused Variables
44
2.2 Data Types
44
2.2.1 Primitive Data Types
44
2.2.2 Compound Data Types
46
2.2.3 Text-Related Types
48
2.3 Functions
49
2.4 Code Blocks
51
2.5 Practice Exercises
52
2.6 Solutions
56
2.7 Summary
59
3 Conditionals and Control Flow
61
3.1 Conditionals
61
3.1.1 If Else
61
3.1.2 If Else If Ladder
63
3.1.3 Match
64
3.2 Control Flow
66
3.2.1 Simple Loops
67
3.2.2 For and While Loops
68
3.3 Comments, Outputs, and Inputs
68
3.3.1 Comments
69
3.3.2 Formatting Outputs with Escape Sequences and Arguments
69
3.3.3 User Input
71
3.4 Practice Exercises
71
3.5 Solutions
74
3.6 Summary
78
4 Ownership
79
4.1 Ownership Basics
79
4.1.1 Values Must Have a Single Owner
79
4.1.2 When Owner Goes Out of Scope, the Value Is Cleaned Up
83
4.2 Ownership in Functions
84
4.2.1 Functions Taking Ownership
84
4.2.2 Function Returning Ownership
85
4.2.3 Function Taking and Returning Ownership
85
4.3 Borrowing Basics
87
4.3.1 Why Borrowing?
87
4.3.2 Borrowing Rules
87
4.3.3 Copying of References
91
4.4 Borrowing in Functions
92
4.5 Dereferencing
97
4.6 Mutable and Immutable Binding of References
98
4.7 Practice Exercises
101
4.8 Solutions
104
4.9 Summary
106
5 Custom and Library-Provided Useful Types
107
5.1 Structs
107
5.1.1 Defining Structs
107
5.1.2 Instantiating Struct Instances
108
5.1.3 Ownership Considerations
110
5.1.4 Tuple Structs
110
5.1.5 Unit Structs
111
5.1.6 Adding Functionality to Structs
111
5.1.7 Associated Functions
115
5.2 Enums
117
5.2.1 Why Use Enums?
117
5.2.2 Defining Enums
118
5.2.3 Implementation Blocks for Enums
119
5.2.4 Adding Data to Enum Variants
120
5.3 Option
121
5.3.1 Why Use Option?
121
5.3.2 Defining the Option Enum
122
5.3.3 Matching on Option
123
5.3.4 Use of If Let with Option
124
5.4 Result
125
5.4.1 Why Use Result?
125
5.4.2 Defining the Result Enum
125
5.4.3 Matching on Result
126
5.4.4 The ? Operator
128
5.5 HashMaps
129
5.6 HashSets
131
5.7 Practice Exercises
133
5.8 Solutions
137
5.9 Summary
142
6 Organizing Your Code
143
6.1 Code Organization
143
6.2 Module Basics
146
6.2.1 Motivating Example for Modules
147
6.2.2 Creating Modules
148
6.2.3 Relative and Absolute Paths of Items
150
6.2.4 Privacy in Modules
152
6.2.5 The Use Declaration for Importing or Bringing Items into Scope
155
6.3 Visualizing and Organizing Modules
157
6.3.1 Cargo Modules for Visualizing Module Hierarchy
157
6.3.2 Organizing Code Using a Typical File System
159
6.4 Re-Exporting and Privacy
164
6.4.1 Re-Exporting with a Pub Use Expression
164
6.4.2 Privacy of Structs
167
6.5 Using External Dependencies
170
6.6 Publishing Your Crate
173
6.6.1 Creating an Account on crates.io
173
6.6.2 Adding Documentation before Publishing
174
6.6.3 Publishing Your Crate
176
6.7 Practice Exercises
177
6.8 Solutions
180
6.9 Summary
182
7 Testing Code
185
7.1 Unit Testing
185
7.1.1 A Typical Test Case
185
7.1.2 Writing a Test Function
186
7.1.3 Executing Tests
188
7.1.4 Testing with Result Enum
190
7.1.5 Testing Panics
192
7.2 Controlling Test Execution
195
7.3 Integration Tests
197
7.4 Benchmark Testing
200
7.5 Practice Exercises
204
7.6 Solutions
207
7.7 Summary
210
PART II Intermediate Language Concepts
211
8 Flexibility and Abstraction with Generics and Traits
213
8.1 Generics
213
8.1.1 Basics of Generics
213
8.1.2 Generics in Implementation Blocks
215
8.1.3 Multiple Implementations for a Type: Generics versus Concrete
216
8.1.4 Duplicate Definitions in Implementation Blocks
217
8.1.5 Generics and Free Functions
218
8.1.6 Monomorphization
219
8.2 Traits
220
8.2.1 Motivating Example for Traits
220
8.2.2 Traits Basics
222
8.2.3 Default Implementations
224
8.2.4 Trait Bounds
225
8.2.5 Supertraits
230
8.2.6 Trait Objects
233
8.2.7 Derived Traits
237
8.2.8 Marker Traits
239
8.2.9 Associated Types in Traits
240
8.3 Choosing between Associated Types and Generic Types
244
8.4 Practice Exercises
249
8.5 Solutions
253
8.6 Summary
257
9 Functional Programming Aspects
259
9.1 Closures
259
9.1.1 Motivating Example for Closures
259
9.1.2 Basic Syntax
260
9.1.3 Passing Closures to Functions
261
9.1.4 Capturing Variables from the Environment
262
9.2 Function Pointers
265
9.3 Iterators
270
9.3.1 The Iterator Trait
270
9.3.2 IntoIterator
273
9.3.3 Iterating over Collections
277
9.4 Combinators
279
9.5 Iterating through Option
283
9.6 Practice Exercises
286
9.7 Solutions
291
9.8 Summary
296
10 Memory Management Features
297
10.1 Lifetimes
297
10.1.1 Concrete Lifetimes
297
10.1.2 Generic Lifetimes
302
10.1.3 Static Lifetimes
306
10.1.4 Lifetime Elision
307
10.1.5 Lifetimes and Structs
310
10.2 Smart Pointers
313
10.2.1 Box Smart Pointer
313
10.2.2 Rc Smart Pointer
318
10.2.3 RefCell Smart Pointer
322
10.3 Deref Coercion
327
10.4 Practice Exercises
329
10.5 Solutions
333
10.6 Summary
337
11 Implementing Typical Data Structures
339
11.1 Singly Linked List
339
11.1.1 Implementation by Modifying the List Enum
340
11.1.2 Resolving Issues with the Implementation
341
11.1.3 Refining the Next Field
343
11.1.4 Adding Elements
345
11.1.5 Removing Elements
348
11.1.6 Printing Singly Linked Lists
350
11.2 Doubly Linked List
351
11.2.1 Setting Up the Basic Data Structure
352
11.2.2 Adding Elements
353
11.2.3 Adding a New Constructor Function for Node
356
11.2.4 Removing Elements
357
11.2.5 Printing Doubly Linked Lists
361
11.3 Reference Cycles Creating Memory Leakage
362
11.4 Practice Exercises
368
11.5 Solutions
370
11.6 Summary
375
12 Useful Patterns for Handling Structs
377
12.1 Initializing Struct Instances
377
12.1.1 New Constructors
377
12.1.2 Default Constructors
380
12.2 Builder Pattern
381
12.2.1 Motivating Example for the Builder Pattern
382
12.2.2 Solving the Proliferation of Constructors
384
12.3 Simplifying Structs
388
12.4 Practice Exercises
392
12.5 Solutions
394
12.6 Summary
398
PART III Advanced Language Concepts
399
13 Understanding Size in Rust
401
13.1 Sized and Unsized Types
401
13.1.1 Examples of Sized Types
402
13.1.2 Examples of Unsized Types
403
13.2 References to Unsized Types
405
13.3 Sized and Optionally Sized Traits
408
13.3.1 Opting Out of Sized Trait
409
13.3.2 Generic Bound of Sized Traits
410
13.3.3 Flexible Generic Function with Optionally Sized Trait
413
13.4 Unsized Coercion
415
13.4.1 Deref Coercion
415
13.4.2 Unsized Coercion
416
13.4.3 Unsized Coercion with Traits
416
13.5 Zero-Sized Types
418
13.5.1 Never Type
418
13.5.2 Unit Type
421
13.5.3 Unit Structs
424
13.5.4 PhantomData
427
13.6 Practice Exercises
428
13.7 Solutions
430
13.8 Summary
432
14 Concurrency
433
14.1 Thread Basics
433
14.1.1 Fundamental Concepts
433
14.1.2 Creating Threads
434
14.1.3 Thread Completion Using Sleep
435
14.1.4 Thread Completion Using Join
436
14.2 Ownership in Threads
438
14.3 Thread Communication
440
14.3.1 Message Passing
440
14.3.2 Sharing States
448
14.4 Synchronization through Barriers
454
14.4.1 Motivating Example for Barriers
454
14.4.2 Synchronizing Threads Using Barriers
456
14.5 Scoped Threads
458
14.6 Thread Parking
461
14.6.1 Motivating Example for Thread Parking
462
14.6.2 Temporarily Blocking a Thread
464
14.6.3 Park Timeout Function
465
14.7 Async Await
466
14.7.1 Creating Async Functions
466
14.7.2 Deriving Futures to Completion with the Await Method
467
14.7.3 Tokio
468
14.8 Web Scraping Using Threads
474
14.9 Practice Exercises
477
14.10 Solutions
481
14.11 Summary
485
15 Macros
487
15.1 Macro Basics
487
15.1.1 Basic Syntax
487
15.1.2 Matching Pattern in the Rule
489
15.1.3 Captures
490
15.1.4 Strict Matching
491
15.1.5 Macro Expansion
492
15.2 Capturing Types
494
15.2.1 Type Capture
494
15.2.2 Identifiers Capture
497
15.3 Repeating Patterns
500
15.4 Practice Exercises
505
15.5 Solutions
506
15.6 Summary
508
16 Web Programming
509
16.1 Creating a Server
509
16.1.1 Server Basics
509
16.1.2 Implementing a Server
510
16.1.3 Handling Multiple Connections
511
16.1.4 Adding a Connection Handling Function
513
16.2 Making Responses
515
16.2.1 Response Syntax
515
16.2.2 Responding with Valid HTML
517
16.2.3 Returning Different Responses
518
16.3 Multithreaded Server
522
16.4 Practice Exercises
525
16.5 Solutions
525
16.6 Summary
528
17 Text Processing, File Handling, and Directory Management
529
17.1 Basic File Handling
529
17.1.1 Creating a File and Adding Content
529
17.1.2 Appending a File
531
17.1.3 Storing the Results
532
17.1.4 Reading from a File
533
17.2 Path- and Directory-Related Functions
534
17.2.1 Working with Paths
534
17.2.2 Working with Directories
536
17.3 Regular Expressions Basics
539
17.3.1 Basic Methods
539
17.3.2 Dot and Character Ranges
541
17.3.3 Starting and Ending Anchors
542
17.3.4 Word Boundaries
543
17.3.5 Quantifiers, Repetitions, and Capturing Groups
544
17.4 String Literals
547
17.4.1 Working with Raw String Literals
548
17.4.2 Parsing JSON
549
17.4.3 Using a Hash within a String
549
17.5 Practice Exercises
550
17.6 Solutions
552
17.7 Summary
554
18 Practical Problems
557
18.1 Problem 1: Search Results with Word Groupings
557
18.1.1 Solution Setup
557
18.1.2 Implementation
558
18.2 Problem 2: Product Popularity
561
18.2.1 Solution Setup
561
18.2.2 Implementation
562
18.3 Problem 3: Highest Stock Price
564
18.3.1 Solution Setup
565
18.3.2 Implementation
565
18.4 Problem 4: Identify Time Slots
568
18.4.1 Solution Setup
568
18.4.2 Implementation
570
18.5 Problem 5: Item Suggestions
572
18.5.1 Solution Setup
572
18.5.2 Implementation
573
18.6 Problem 6: Items in Range Using Binary Search Trees
575
18.6.1 Solution Setup
575
18.6.2 Basic Data Structure
578
18.6.3 Implementation
580
18.7 Problem 7: Fetching Top Products
582
18.7.1 Solution Setup
583
18.7.2 Implementation
585
18.8 Problem 8: Effective Storage and Retrieval
589
18.8.1 Solution Setup
589
18.8.2 Basic Data Structure
590
18.8.3 Implementation
592
18.9 Problem 9: Most Recently Used Product
595
18.9.1 Solution Setup
596
18.9.2 Basic Data Structure
598
18.9.3 Implementation
601
18.10 Problem 10: Displaying Participants in an Online Meeting
607
18.10.1 Solution Setup
608
18.10.2 Basic Data Structure
610
18.10.3 Implementation
612
18.11 Summary
615
The Author
617
Index
619