Get "close to the machine" by programming with Rust! Discover how to effectively use this low-level language to create fast and efficient code. Set up Rust, compile your first program, and learn the language essentials: variables, functions, conditionals, and more. Walk through Rust's unique ownership model and modular system, and then move on to more complex features, from flexibility and abstraction to web programming and text processing. Numerous code examples and exercises make this your go-to practical resource for Rust!
In this book, you'll learn about:
a. Language Basics
Take your first steps with Rust! After installation and setup in Visual Studio Code, walk through the language building blocks, from variables to functions to enums. Learn how to implement ownership, define types, test your code, and more.
b. Intermediate Programming
Expand your toolkit for Rust programming! Write flexible code with generics and traits, incorporate features of functional programming, manage memory, implement data structures, and handle structs.
c. Advanced Topics
Become a Rust master! Tackle advanced topics, including sized and unsized types, concurrency via threading, the powerful macro system, web programming, text processing, and file and directory handling.
Highlights include:
1) Language basics
2) Control flow
3) Ownership
4) Structs and enums
5) Generics and traits
6) Modular structure
7) Memory management
8) Testing
9) Concurrency
10) Functional programming
11) Web programming
12) Text processing and file handling
Sprache
Verlagsort
Verlagsgruppe
Rheinwerk Publishing Inc.
Editions-Typ
Produkt-Hinweis
Maße
Höhe: 250 mm
Breite: 181 mm
Dicke: 35 mm
Gewicht
ISBN-13
978-1-4932-2687-0 (9781493226870)
Schweitzer Klassifikation
Autor*in
Dr. Nouman Azam is an associate professor of computer science at the National University of Computer and Emerging Sciences. He also teaches online programming courses about Rust and MATLAB, and reaches a community of more than 50,000 students.
He received his PhD in computer science from the University of Regina in Canada. Prior to that, he completed his MSc in computer software engineering from the National University of Sciences and Technology, Pakistan, and his BSc in computer sciences from the National University of Computer and Emerging Sciences, Pakistan.
Dr. Azam's research interests include game theory, rough sets, conflict analysis, and group decision making.
... 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.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.2 ... Data Types ... 44
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.2 ... Control Flow ... 66
3.3 ... Comments, Outputs, and Inputs ... 68
3.4 ... Practice Exercises ... 71
3.5 ... Solutions ... 74
3.6 ... Summary ... 78
4 ... Ownership ... 79
4.1 ... Ownership Basics ... 79
4.2 ... Ownership in Functions ... 84
4.3 ... Borrowing Basics ... 87
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.2 ... Enums ... 117
5.3 ... Option ... 121
5.4 ... Result ... 125
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.3 ... Visualizing and Organizing Modules ... 157
6.4 ... Re-Exporting and Privacy ... 164
6.5 ... Using External Dependencies ... 170
6.6 ... Publishing Your Crate ... 173
6.7 ... Practice Exercises ... 177
6.8 ... Solutions ... 180
6.9 ... Summary ... 182
7 ... Testing Code ... 185
7.1 ... Unit Testing ... 185
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.2 ... Traits ... 220
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.2 ... Function Pointers ... 265
9.3 ... Iterators ... 270
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.2 ... Smart Pointers ... 313
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.2 ... Doubly Linked List ... 351
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.2 ... Builder Pattern ... 381
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.2 ... References to Unsized Types ... 405
13.3 ... Sized and Optionally Sized Traits ... 408
13.4 ... Unsized Coercion ... 415
13.5 ... Zero-Sized Types ... 418
13.6 ... Practice Exercises ... 428
13.7 ... Solutions ... 430
13.8 ... Summary ... 432
14 ... Concurrency ... 433
14.1 ... Thread Basics ... 433
14.2 ... Ownership in Threads ... 438
14.3 ... Thread Communication ... 440
14.4 ... Synchronization through Barriers ... 454
14.5 ... Scoped Threads ... 458
14.6 ... Thread Parking ... 461
14.7 ... Async Await ... 466
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.2 ... Capturing Types ... 494
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.2 ... Making Responses ... 515
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.2 ... Path- and Directory-Related Functions ... 534
17.3 ... Regular Expressions Basics ... 539
17.4 ... String Literals ... 547
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.2 ... Problem 2: Product Popularity ... 561
18.3 ... Problem 3: Highest Stock Price ... 564
18.4 ... Problem 4: Identify Time Slots ... 568
18.5 ... Problem 5: Item Suggestions ... 572
18.6 ... Problem 6: Items in Range Using Binary Search Trees ... 575
18.7 ... Problem 7: Fetching Top Products ... 582
18.8 ... Problem 8: Effective Storage and Retrieval ... 589
18.9 ... Problem 9: Most Recently Used Product ... 595
18.10 ... Problem 10: Displaying Participants in an Online Meeting ... 607
18.11 ... Summary ... 615
... The Author ... 617
... Index ... 619