📄 Page
1
(This page has no text content)
📄 Page
2
Rheinwerk Computing Explore more of the Rheinwerk Computing library! The Rheinwerk Computing series offers new and established professionals comprehen- sive guidance to enrich their skillsets and enhance their career prospects. Our publica- tions are written by the leading experts in their fields. Each book is detailed and hands-on to help readers develop essential, practical skills that they can apply to their daily work. www.rheinwerk-computing.com Christian Wenz, Tobias Hauser PHP and MySQL: The Comprehensive Guide 2025, 1005 pages, paperback and e-book www.rheinwerk-computing.com/6022 Torsten T. Will C++: The Comprehensive Guide 2025, 1089 pages, paperback and e-book www.rheinwerk-computing.com/5927 Johannes Ernesti, Peter Kaiser Python 3: The Comprehensive Guide 2022, 1036 pages, paperback and e-book www.rheinwerk-computing.com/5566 Philip Ackermann JavaScript: The Comprehensive Guide 2022, 982 pages, paperback and e-book www.rheinwerk-computing.com/5554 Sebastian Springer Node.js: The Comprehensive Guide 2022, 834 pages, paperback and e-book www.rheinwerk-computing.com/5556
📄 Page
3
Nouman Azam Rust The Practical Guide
📄 Page
4
Imprint This e-book is a publication many contributed to, specifically: Editor Megan Fuerst Acquisitions Editor Hareem Shafi Copyeditor Yvette Chin Cover Design Graham Geary Photo Credit iStockphoto: 168329140/© ilbusca; Shutterstock: 1126625519/© Jackie Niam Layout Design Vera Brauner Production E-Book Hannah Lane Typesetting E-Book SatzPro, Germany We hope that you liked this e-book. Please share your feedback with us and read the Service Pages to find out how to contact us. The Library of Congress Cataloging-in-Publication Control Number for the printed edition is as follows: 2024061177 ISBN 978-1-4932-2687-0 (print) ISBN 978-1-4932-2688-7 (e-book) ISBN 978-1-4932-2689-4 (print and e-book) 1st edition 2025 © 2025 by: Rheinwerk Publishing, Inc. 2 Heritage Drive, Suite 305 Quincy, MA 02171 USA info@rheinwerk-publishing.com +1.781.228.5070 Represented in the E.U. by: Rheinwerk Verlag GmbH Rheinwerkallee 4 53227 Bonn Germany service@rheinwerk-verlag.de +49 (0) 228 42150-0
📄 Page
5
(This page has no text content)
📄 Page
6
Contents Preface ....................................................................................................................................................... 19 PART I Basic Programming with Rust 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 ................................................................................................................... 527
📄 Page
7
Contents2.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 .......................................................................................................................... 978
📄 Page
8
Contents4.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 ................................................................................................................................... 1429
📄 Page
9
Contents6 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 ................................................................................................................... 20410
📄 Page
10
Contents7.6 Solutions .................................................................................................................................... 207 7.7 Summary ................................................................................................................................... 210 PART II Intermediate Language Concepts 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 .............................................................................................................. 26011
📄 Page
11
Contents9.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 ................................................... 34112
📄 Page
12
Contents11.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 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 ................................................................................. 40313
📄 Page
13
Contents13.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 ......................................................................................... 46514
📄 Page
14
Contents14.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 ............................................................................. 51715
📄 Page
15
Contents16.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 ......................................................................................................... 55716
📄 Page
16
Contents18.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 Service Pages ..................................................................................................................................... II Legal Notes ......................................................................................................................................... I 17
📄 Page
17
(This page has no text content)
📄 Page
18
Preface Welcome to this practical guide for Rust programming. Since its inception, Rust has rapidly gained popularity due to its emphasis on memory safety, performance, and concurrency. As the demand for safer and more efficient systems programming grows, developers and organizations have increasingly turned to Rust to build reliable soft- ware. This book is designed to bridge the gap between Rust’s powerful features and practical implementation, offering a structured and hands-on approach to mastering the language. This book provides a deep dive into Rust’s core concepts, covering everything from ownership and borrowing to advanced features like smart pointers, concurrency, and metaprogramming. We explore how Rust enables developers to write safe and efficient code without compromising performance, making it an excellent choice for a wide range of applications—from embedded systems to web services. Whether you’re new to Rust or looking to refine your expertise, this book serves as a comprehensive re- source, equipping you with knowledge and tools to build robust and high-performance software in Rust. The Purpose of This Book For many learning Rust, it quickly becomes apparent that while numerous resources exist, few provide in-depth examples that clarify complex concepts. Rust’s steep learn- ing curve, and particularly its unique ownership system and borrow checker, often makes it difficult for beginners to write even simple programs without encountering compiler errors. Understanding why the compiler rejects code and how to fix these issues can be overwhelming. Many existing resources are scattered or lack a structured approach, making it difficult to understand how to progress systematically through the language. This book addresses these gaps by organizing material into meaningful sec- tions, arranged in a progressive manner to help you build understanding step by step. Through clear explanations, practical examples, and structured learning paths, this book aims to provide a smoother and more intuitive journey into Rust. The Structure of This Book This book is organized into three parts: Part I, Basic Programming with Rust; Part II, Intermediate Language Concepts; and Part III, Advanced Language Concepts. Part I spans Chapter 1 to Chapter 7. This part of the book starts with an introduction to Rust programming, including how to install and set up the coding environment and19Personal Copy for Jaleel Hussain, alex76alex43@gmail.com
📄 Page
19
Prefacesome tips on how to take full advantage of the material in the book. Next, it introduces basic programming constructs such as variables, data types, functions, conditionals, and control flow. Following this, it delves into Rust’s unique ownership model, cover- ing borrowing and dereferencing. The last two chapters in this part cover custom types like structs and enums, useful library types such as Option and Result, and practical aspects of organizing and testing code. Part II includes Chapter 8 to Chapter 12. This part of the book expands on generics and traits for flexibility and abstraction, followed by functional programming aspects like closures, functional pointers, and iterators. It also covers memory management fea- tures, including lifetimes and smart pointers, and guides you through implementing common data structures and useful patterns for handling structs. Part III starts with Chapter 13 and ends at Chapter 18. This part tackles complex topics like size in Rust, concurrency with threads and asynchronous programming, and the powerful macro system. This part also addresses real-life problems requiring data structure-heavy solutions, web programming fundamentals, and text processing, file handling, and directory management. Let’s take a look at the topics covered in each chapter of this book: Chapter 1: Introduction This chapter lays the groundwork for Rust development by covering installation, setup, and compilation of your first Rust program. It ensures a smooth start by guid- ing you through configuring Visual Studio Code (VS Code) and making the most of the book’s material. Chapter 2: Variables, Data Types, and Functions This chapter introduces variables and includes topics such as mutability, scope, and shadowing, alongside a deep dive into primitive and compound data types. It also explores functions and code blocks, establishing a strong foundation for program development. Chapter 3: Conditionals and Control Flow You’ll explore Rust’s control flow constructs, including if-else statements and match. The chapter also covers looping mechanisms such as for, while, and infinite loops, along with essential input/output handling techniques. Chapter 4: Ownership This chapter explains Rust's ownership system, focusing on how memory is man- aged through ownership, borrowing, and dereferencing. By mastering these core principles, you’ll learn to write memory-safe and efficient Rust programs. Chapter 5: Custom and Library-Provided Useful Types This chapter covers defining custom types using structs and enums while also exploring key library types like Option, Result, HashMap, and HashSet. Understanding these types helps with building efficient and structured Rust applications.20 © 2025 by Rheinwerk Publishing Inc., Boston (MA)
📄 Page
20
Preface Chapter 6: Organizing Your Code You’ll learn best practices for structuring Rust projects using modules, organizing code logically, and managing privacy. The chapter also covers re-exporting, incorpo- rating external dependencies, and publishing crates. This ensures you can develop scalable and well-structured Rust applications. Chapter 7: Testing Code This chapter explores the fundamentals of testing in Rust, covering unit tests and integration testing. It also delves into controlling test execution and testing config- urations. By the end, you’ll understand how to write reliable and efficient Rust pro- grams through rigorous testing. Chapter 8: Flexibility and Abstraction with Generics and Traits This chapter introduces generics for writing reusable code and traits for defining shared behavior across types. You’ll also learn about trait bounds, supertraits, trait objects, and associated types to build flexible and abstract designs. Chapter 9: Functional Programming Aspects Rust’s functional programming features are explored in this chapter, including clo- sures, functional pointers, and iterators. The chapter also covers combinators and iterating through Option types for expressive and concise programming. Chapter 10: Memory Management Features This chapter covers lifetimes, lifetime elision, and smart pointers like Box, Rc, and RefCell. These concepts help ensure safe memory management and efficient resource allocation in Rust. Chapter 11: Implementing Typical Data Structures You’ll learn to implement singly and doubly linked lists while understanding refer- ence cycles and memory leaks. The chapter provides hands-on examples to build efficient and safe data structures. Chapter 12: Useful Patterns for Handling Structs This chapter explores struct initialization patterns, including the builder pattern and techniques for simplifying struct design. These approaches make Rust code more maintainable and flexible. Chapter 13: Understanding Size in Rust This chapter explores the distinction between sized and unsized types, including ref- erences to unsized types and their memory implications. It also covers unsized coer- cion, optionally sized traits, and zero-sized types like the never type, unit type, unit structs, and phantom data. Chapter 14: Concurrency You’ll learn about concurrency in Rust, including threads, ownership in multi- threaded contexts, and communication techniques like message passing and shared states. This chapter also introduces synchronization methods, async/await, Tokio tasks, and practical applications such as web scraping.21Personal Copy for Jaleel Hussain, alex76alex43@gmail.com