Mastering Computer Science Series Preface |
|
xvii | |
About the Editor |
|
xix | |
|
Chapter 1 Getting Started with Rust |
|
|
1 | (16) |
|
|
1 | (8) |
|
|
2 | (1) |
|
Rust's Increasing Popularity |
|
|
2 | (1) |
|
What Makes the Rust Programming Language Unique? |
|
|
3 | (1) |
|
Concurrent Programming Has Been Simplified |
|
|
4 | (1) |
|
Some Challenges to Overcome while Programming in Rust |
|
|
4 | (1) |
|
What Is the Purpose of Rust? |
|
|
5 | (1) |
|
|
6 | (1) |
|
|
6 | (3) |
|
|
9 | (1) |
|
Rust in Windows Installation |
|
|
9 | (1) |
|
Installing Rust on Linux or macOS |
|
|
10 | (1) |
|
Updating and Uninstalling Rust |
|
|
10 | (1) |
|
|
10 | (1) |
|
Procedure for Creating, Compiling, and Running the Program |
|
|
11 | (1) |
|
Reasons Why Rust Is The Way Of The Future |
|
|
11 | (6) |
|
|
12 | (1) |
|
Advantages of Rust vs. C++ |
|
|
12 | (1) |
|
Advantages of Rust vs. Java |
|
|
12 | (1) |
|
Advantages of Rust vs. Python |
|
|
12 | (1) |
|
Advantages of Rust vs. Go |
|
|
12 | (1) |
|
What Makes Rust the Future? |
|
|
13 | (1) |
|
Rust Increased the Safety of Memory |
|
|
13 | (1) |
|
Rust's Community Is Expanding |
|
|
14 | (1) |
|
Rust Is Quick and Adaptable |
|
|
14 | (1) |
|
Rust Has a Wide Range of Applications |
|
|
15 | (1) |
|
Rust Is Used by Several Large Companies |
|
|
15 | (2) |
|
Chapter 2 Common Programming Concepts |
|
|
17 | (46) |
|
|
18 | (1) |
|
|
18 | (1) |
|
|
18 | (1) |
|
|
19 | (1) |
|
|
19 | (1) |
|
Variables and Constants: What Are the Differences? |
|
|
19 | (1) |
|
|
20 | (9) |
|
|
21 | (1) |
|
|
21 | (1) |
|
|
22 | (1) |
|
|
22 | (1) |
|
|
22 | (1) |
|
|
23 | (1) |
|
|
23 | (1) |
|
|
24 | (1) |
|
|
24 | (1) |
|
|
24 | (1) |
|
|
24 | (1) |
|
|
25 | (1) |
|
|
25 | (1) |
|
|
26 | (1) |
|
Accessing the Array Elements |
|
|
27 | (1) |
|
Invalid Array Element Access |
|
|
28 | (1) |
|
|
29 | (2) |
|
Constant Naming Convention in Rust |
|
|
29 | (1) |
|
|
29 | (1) |
|
Variable and Constant Shadowing |
|
|
30 | (1) |
|
|
31 | (7) |
|
|
31 | (1) |
|
|
32 | (1) |
|
|
32 | (1) |
|
String Object - Common Methods |
|
|
33 | (1) |
|
|
33 | (1) |
|
Illustration: to string() |
|
|
34 | (1) |
|
|
34 | (1) |
|
|
34 | (1) |
|
|
35 | (1) |
|
|
35 | (1) |
|
|
35 | (1) |
|
|
35 | (1) |
|
Illustration: split whitespace() |
|
|
36 | (1) |
|
Illustration: split() string |
|
|
36 | (1) |
|
|
37 | (1) |
|
Concatenation of the Strings with + Operator |
|
|
37 | (1) |
|
Illustration: String Concatenation |
|
|
37 | (1) |
|
Illustration: Type Casting |
|
|
37 | (1) |
|
Illustration: Format! Macro |
|
|
38 | (1) |
|
|
38 | (2) |
|
|
38 | (1) |
|
|
39 | (1) |
|
|
39 | (1) |
|
|
39 | (1) |
|
|
40 | (5) |
|
|
40 | (1) |
|
|
40 | (1) |
|
|
41 | (1) |
|
|
41 | (1) |
|
|
41 | (1) |
|
|
42 | (1) |
|
|
42 | (1) |
|
|
43 | (1) |
|
|
43 | (1) |
|
Using an "if" statement within a "let" statement |
|
|
44 | (1) |
|
|
44 | (1) |
|
|
45 | (3) |
|
|
45 | (1) |
|
|
46 | (1) |
|
|
46 | (1) |
|
|
46 | (1) |
|
|
47 | (1) |
|
|
47 | (1) |
|
|
48 | (1) |
|
|
48 | (2) |
|
|
48 | (1) |
|
Distinctions between the While Loop and For Loop |
|
|
49 | (1) |
|
|
50 | (4) |
|
|
50 | (1) |
|
|
50 | (1) |
|
|
51 | (1) |
|
|
51 | (1) |
|
|
51 | (1) |
|
Returning Value from a Function |
|
|
52 | (1) |
|
|
52 | (1) |
|
Function with the Parameters |
|
|
52 | (1) |
|
|
53 | (1) |
|
|
53 | (1) |
|
Passing String to a Function |
|
|
54 | (1) |
|
|
54 | (1) |
|
|
55 | (1) |
|
|
56 | (1) |
|
|
56 | (7) |
|
|
57 | (1) |
|
Array Declaration and Initialization |
|
|
57 | (1) |
|
|
57 | (1) |
|
Illustration: Simple Array |
|
|
58 | (1) |
|
Illustration: Array without Data Type |
|
|
58 | (1) |
|
Illustration: Default Values |
|
|
58 | (1) |
|
Illustration: Array with for Loop |
|
|
58 | (1) |
|
Illustration: Using the iter() Function |
|
|
59 | (1) |
|
Illustration: Mutable Array |
|
|
59 | (1) |
|
Passing Arrays as Parameters to the Functions |
|
|
59 | (1) |
|
Illustration: Pass by Value |
|
|
59 | (1) |
|
Illustration: Pass by Reference |
|
|
60 | (1) |
|
The Array Declaration and Constants |
|
|
60 | (3) |
|
Chapter 3 Understanding Ownership |
|
|
63 | (26) |
|
What Exactly Is Ownership? |
|
|
63 | (13) |
|
|
64 | (1) |
|
Important Ownership Concepts |
|
|
65 | (1) |
|
|
65 | (1) |
|
|
66 | (1) |
|
|
66 | (1) |
|
|
67 | (2) |
|
Ways of Variables and Data Interact: Move |
|
|
69 | (3) |
|
Variables and Data Interactions: Clone |
|
|
72 | (1) |
|
|
73 | (1) |
|
|
73 | (1) |
|
|
74 | (2) |
|
References And Borrowing In Rust |
|
|
76 | (5) |
|
|
76 | (2) |
|
|
78 | (1) |
|
Restrictions of the Mutable References |
|
|
78 | (1) |
|
|
79 | (1) |
|
The Referencing Guidelines |
|
|
80 | (1) |
|
|
81 | (8) |
|
|
81 | (2) |
|
|
83 | (4) |
|
Literals Are String Slices |
|
|
87 | (1) |
|
String Slices as Parameters |
|
|
87 | (1) |
|
|
88 | (1) |
|
Chapter 4 Using Structs for Related Data |
|
|
89 | (22) |
|
What Is The Definition Of A Structure? |
|
|
89 | (6) |
|
When the Variables and Fields Have the Same Name, Use the Field init Shorthand |
|
|
91 | (1) |
|
Using Struct Update Syntax to Create Instances from Other Instances |
|
|
92 | (1) |
|
The Tuple Structs without Named Fields to Create Different Types |
|
|
93 | (1) |
|
Structs that Look Like Units but Don't Have Any Fields |
|
|
93 | (1) |
|
|
94 | (1) |
|
|
95 | (8) |
|
An Example of a Structs Program |
|
|
97 | (1) |
|
Refactoring with the Tuples |
|
|
98 | (1) |
|
Using Structs for Refactoring: Adding Additional Meaning |
|
|
99 | (1) |
|
Using Derived Traits to Add Useful Functionality |
|
|
100 | (3) |
|
|
103 | (8) |
|
|
104 | (2) |
|
Methods with More Parameters |
|
|
106 | (2) |
|
|
108 | (1) |
|
|
108 | (3) |
|
Chapter 5 Enums and Pattern Matching |
|
|
111 | (18) |
|
|
117 | (2) |
|
|
112 | (4) |
|
The Advantages of the Option Enum over Null Values |
|
|
116 | (3) |
|
The Match Control Flow Operator |
|
|
119 | (7) |
|
Patterns that Bind to Values |
|
|
120 | (2) |
|
Matching with the Option<T> |
|
|
122 | (1) |
|
|
123 | (1) |
|
Catch-all Patterns and the_Placeholder |
|
|
124 | (2) |
|
Concise Control Flow With If Let |
|
|
126 | (3) |
|
Chapter 6 Packages, Crates, and Modules |
|
|
129 | (20) |
|
|
131 | (1) |
|
Defining Modules To The Control Scope And Privacy |
|
|
132 | (2) |
|
Paths For Referring To An Item In The Module Tree |
|
|
134 | (7) |
|
Exposing Paths with pub Keyword |
|
|
137 | (2) |
|
Starting Relative Paths with super |
|
|
139 | (1) |
|
Making Structs and Enums Public |
|
|
139 | (2) |
|
Bringing Paths Into The Scope With The Use Keyword |
|
|
141 | (5) |
|
Creating the Idiomatic use Paths |
|
|
143 | (1) |
|
Providing New Names with the as Keyword |
|
|
144 | (1) |
|
Re-exporting Names with pub use |
|
|
145 | (1) |
|
Separating Modules Into Different Files |
|
|
146 | (3) |
|
|
149 | (22) |
|
|
149 | (5) |
|
Unrecoverable Errors with panic! |
|
|
150 | (1) |
|
Unwinding Stack or Aborting in Response to a Panic |
|
|
150 | (1) |
|
|
151 | (3) |
|
Recoverable Errors With The Result |
|
|
154 | (10) |
|
Shortcuts for the Panic on Error: unwrap and expect |
|
|
158 | (1) |
|
|
159 | (2) |
|
Shortcut for Propagating Errors: The ? Operator |
|
|
161 | (2) |
|
The ? Operator Can Be Used in Functions that Return Result |
|
|
163 | (1) |
|
To panic! Or not to panic! |
|
|
164 | (7) |
|
Examples, Prototype Code, and Tests |
|
|
165 | (1) |
|
Cases in Which We Have More Information than the Compiler |
|
|
165 | (1) |
|
Guidelines for the Error Handling |
|
|
166 | (1) |
|
Creating the Custom Types for Validation |
|
|
167 | (4) |
|
Chapter 8 Generic Types, Traits, and Lifetimes |
|
|
171 | (18) |
|
|
171 | (13) |
|
|
171 | (4) |
|
In the Struct Definitions |
|
|
175 | (2) |
|
|
177 | (3) |
|
Performance of Code Using Generics |
|
|
180 | (1) |
|
Traits: Defining Shared Behavior |
|
|
181 | (1) |
|
|
181 | (1) |
|
Implementing a Trait on a Type |
|
|
182 | (2) |
|
Validating References With Lifetimes |
|
|
184 | (5) |
|
Preventing Dangling References with Lifetimes |
|
|
185 | (1) |
|
|
186 | (1) |
|
Generic Lifetimes in the Functions |
|
|
187 | (2) |
|
Chapter 9 I/O Project: Building a Command Line Program |
|
|
189 | (24) |
|
Accepting The Command Line Arguments |
|
|
190 | (2) |
|
Saving the Argument Values in the Variables |
|
|
192 | (1) |
|
|
192 | (8) |
|
Refactoring to Improve Modularity and Error Handling |
|
|
194 | (1) |
|
Separation of Concerns for Binary Projects |
|
|
195 | (1) |
|
Extracting the Argument Parser |
|
|
196 | (1) |
|
Grouping the Configuration Values |
|
|
197 | (1) |
|
Creating a Constructor for the Config |
|
|
198 | (1) |
|
Fixing the Error Handling |
|
|
199 | (1) |
|
Improving the Error Message |
|
|
199 | (1) |
|
Developing The Library's Functionality With The Test-Driven Development |
|
|
200 | (3) |
|
Working With The Environment Variables |
|
|
203 | (6) |
|
Writing Failing Test for the Case-Insensitive search Function |
|
|
203 | (1) |
|
Implementing search case insensitive Function |
|
|
204 | (5) |
|
Writing Error Messages To The Standard Error Instead Of Standard Output |
|
|
209 | (4) |
|
Checking Where Errors Are Written |
|
|
209 | (1) |
|
Printing Errors to the Standard Error |
|
|
210 | (3) |
|
Chapter 10 Cargo and crates.io |
|
|
213 | (14) |
|
Customizing Builds With The Release Profiles |
|
|
213 | (2) |
|
Publishing A Crate To Crates.IO |
|
|
215 | (6) |
|
Making Useful Documentation Comments |
|
|
215 | (1) |
|
|
216 | (1) |
|
Documentation Comments as Tests |
|
|
217 | (1) |
|
Commenting Contained Items |
|
|
217 | (1) |
|
Exporting a Convenient Public API with the pub use |
|
|
218 | (3) |
|
Setting Up crates.io Account |
|
|
221 | (1) |
|
|
221 | (3) |
|
|
221 | (2) |
|
Creating the Second Package in the Workspace |
|
|
223 | (1) |
|
Installing Binaries From Crates.Io With Cargo Install |
|
|
224 | (1) |
|
Extending Cargo With The Custom Commands |
|
|
225 | (2) |
|
Chapter 11 Concurrency and State |
|
|
227 | (20) |
|
|
227 | (1) |
|
Using Threads To Run Code Simultaneously |
|
|
228 | (5) |
|
Creating New Thread with spawn |
|
|
230 | (1) |
|
Waiting for All the Threads to Finish Using JoinHandle |
|
|
231 | (2) |
|
Using Message Passing To The Transfer Data Between Threads |
|
|
233 | (7) |
|
The Channels and Ownership Transference |
|
|
236 | (2) |
|
Sending Multiple Values and Seeing the Receiver Waiting |
|
|
238 | (1) |
|
Creating Multiple Producers by Cloning the Transmitter |
|
|
239 | (1) |
|
|
240 | (4) |
|
Using Mutexes to Allow Data Access from Only One Thread at a Time |
|
|
240 | (1) |
|
|
241 | (1) |
|
Sharing Mutex<T> Between Multiple Threads |
|
|
242 | (2) |
|
Extensible Concurrency With Sync And Send Traits |
|
|
244 | (3) |
|
Allowing Transference of the Ownership between Threads with Send |
|
|
244 | (1) |
|
Allowing Access from the Multiple Threads with Sync |
|
|
244 | (1) |
|
Implementing the Send and Sync Manually Is Unsafe |
|
|
245 | (2) |
|
Chapter 12 Object-oriented Programming in Rust |
|
|
247 | (18) |
|
Implementing An Object-Oriented Design Pattern |
|
|
247 | (10) |
|
Defining Post and Creating New Instance in the Draft State |
|
|
249 | (1) |
|
Storing the Text of the Post Content |
|
|
250 | (1) |
|
Ensuring Content of a Draft Post Is Empty |
|
|
251 | (1) |
|
Requesting a Review Changes the State of the Post |
|
|
251 | (5) |
|
Trade-offs of the State Pattern |
|
|
256 | (1) |
|
|
257 | (8) |
|
Difference between Macros and Functions |
|
|
258 | (1) |
|
Declarative Macros with macro rules! for General Metaprogramming |
|
|
258 | (2) |
|
Procedural Macros for Generating Code from the Attributes |
|
|
260 | (1) |
|
How to Write a Custom derive Macro |
|
|
261 | (1) |
|
|
262 | (1) |
|
|
262 | (3) |
Appendix A Keywords |
|
265 | (4) |
Appendix B Operators And Symbols |
|
269 | (6) |
Appendix C Derivable Traits |
|
275 | (4) |
Appraisal |
|
279 | (6) |
Bibliography |
|
285 | (6) |
Index |
|
291 | |