Programming Rust, 2nd Edition

Book description

Systems programming provides the foundation for the world's computation. Writing performance-sensitive code requires a programming language that puts programmers in control of how memory, processor time, and other system resources are used. The Rust systems programming language combines that control with a modern type system that catches broad classes of common mistakes, from memory management errors to data races between threads.

With this practical guide, experienced systems programmers will learn how to successfully bridge the gap between performance and safety using Rust. Jim Blandy, Jason Orendorff, and Leonora Tindall demonstrate how Rust's features put programmers in control over memory consumption and processor use by combining predictable performance with memory safety and trustworthy concurrency.

You'll learn:

  • Rust's fundamental data types and the core concepts of ownership and borrowing
  • How to write flexible, efficient code with traits and generics
  • How to write fast, multithreaded code without data races
  • Rust's key power tools: closures, iterators, and asynchronous programming
  • Collections, strings and text, input and output, macros, unsafe code, and foreign function interfaces

This revised, updated edition covers the Rust 2021 Edition.

Table of contents

  1. Preface
    1. Who Should Read This Book
    2. Why We Wrote This Book
    3. Navigating This Book
    4. Conventions Used in This Book
    5. Using Code Examples
    6. O’Reilly Online Learning
    7. How to Contact Us
    8. Acknowledgments
  2. 1. Systems Programmers Can Have Nice Things
    1. Rust Shoulders the Load for You
    2. Parallel Programming Is Tamed
    3. And Yet Rust Is Still Fast
    4. Rust Makes Collaboration Easier
  3. 2. A Tour of Rust
    1. rustup and Cargo
    2. Rust Functions
    3. Writing and Running Unit Tests
    4. Handling Command-Line Arguments
    5. Serving Pages to the Web
    6. Concurrency
      1. What the Mandelbrot Set Actually Is
      2. Parsing Pair Command-Line Arguments
      3. Mapping from Pixels to Complex Numbers
      4. Plotting the Set
      5. Writing Image Files
      6. A Concurrent Mandelbrot Program
      7. Running the Mandelbrot Plotter
      8. Safety Is Invisible
    7. Filesystems and Command-Line Tools
      1. The Command-Line Interface
      2. Reading and Writing Files
      3. Find and Replace
  4. 3. Fundamental Types
    1. Fixed-Width Numeric Types
      1. Integer Types
      2. Checked, Wrapping, Saturating, and Overflowing Arithmetic
      3. Floating-Point Types
    2. The bool Type
    3. Characters
    4. Tuples
    5. Pointer Types
      1. References
      2. Boxes
      3. Raw Pointers
    6. Arrays, Vectors, and Slices
      1. Arrays
      2. Vectors
      3. Slices
    7. String Types
      1. String Literals
      2. Byte Strings
      3. Strings in Memory
      4. String
      5. Using Strings
      6. Other String-Like Types
    8. Type Aliases
    9. Beyond the Basics
  5. 4. Ownership and Moves
    1. Ownership
    2. Moves
      1. More Operations That Move
      2. Moves and Control Flow
      3. Moves and Indexed Content
    3. Copy Types: The Exception to Moves
    4. Rc and Arc: Shared Ownership
  6. 5. References
    1. References to Values
    2. Working with References
      1. Rust References Versus C++ References
      2. Assigning References
      3. References to References
      4. Comparing References
      5. References Are Never Null
      6. Borrowing References to Arbitrary Expressions
      7. References to Slices and Trait Objects
    3. Reference Safety
      1. Borrowing a Local Variable
      2. Receiving References as Function Arguments
      3. Passing References to Functions
      4. Returning References
      5. Structs Containing References
      6. Distinct Lifetime Parameters
      7. Omitting Lifetime Parameters
    4. Sharing Versus Mutation
    5. Taking Arms Against a Sea of Objects
  7. 6. Expressions
    1. An Expression Language
    2. Precedence and Associativity
    3. Blocks and Semicolons
    4. Declarations
    5. if and match
    6. if let
    7. Loops
    8. Control Flow in Loops
    9. return Expressions
    10. Why Rust Has loop
    11. Function and Method Calls
    12. Fields and Elements
    13. Reference Operators
    14. Arithmetic, Bitwise, Comparison, and Logical Operators
    15. Assignment
    16. Type Casts
    17. Closures
    18. Onward
  8. 7. Error Handling
    1. Panic
      1. Unwinding
      2. Aborting
    2. Result
      1. Catching Errors
      2. Result Type Aliases
      3. Printing Errors
      4. Propagating Errors
      5. Working with Multiple Error Types
      6. Dealing with Errors That “Can’t Happen”
      7. Ignoring Errors
      8. Handling Errors in main()
      9. Declaring a Custom Error Type
      10. Why Results?
  9. 8. Crates and Modules
    1. Crates
      1. Editions
      2. Build Profiles
    2. Modules
      1. Nested Modules
      2. Modules in Separate Files
      3. Paths and Imports
      4. The Standard Prelude
      5. Making use Declarations pub
      6. Making Struct Fields pub
      7. Statics and Constants
    3. Turning a Program into a Library
    4. The src/bin Directory
    5. Attributes
    6. Tests and Documentation
      1. Integration Tests
      2. Documentation
      3. Doc-Tests
    7. Specifying Dependencies
      1. Versions
      2. Cargo.lock
    8. Publishing Crates to crates.io
    9. Workspaces
    10. More Nice Things
  10. 9. Structs
    1. Named-Field Structs
    2. Tuple-Like Structs
    3. Unit-Like Structs
    4. Struct Layout
    5. Defining Methods with impl
      1. Passing Self as a Box, Rc, or Arc
      2. Type-Associated Functions
    6. Associated Consts
    7. Generic Structs
    8. Generic Structs with Lifetime Parameters
    9. Generic Structs with Constant Parameters
    10. Deriving Common Traits for Struct Types
    11. Interior Mutability
  11. 10. Enums and Patterns
    1. Enums
      1. Enums with Data
      2. Enums in Memory
      3. Rich Data Structures Using Enums
      4. Generic Enums
    2. Patterns
      1. Literals, Variables, and Wildcards in Patterns
      2. Tuple and Struct Patterns
      3. Array and Slice Patterns
      4. Reference Patterns
      5. Match Guards
      6. Matching Multiple Possibilities
      7. Binding with @ Patterns
      8. Where Patterns Are Allowed
      9. Populating a Binary Tree
    3. The Big Picture
  12. 11. Traits and Generics
    1. Using Traits
      1. Trait Objects
      2. Generic Functions and Type Parameters
      3. Which to Use
    2. Defining and Implementing Traits
      1. Default Methods
      2. Traits and Other People’s Types
      3. Self in Traits
      4. Subtraits
      5. Type-Associated Functions
    3. Fully Qualified Method Calls
    4. Traits That Define Relationships Between Types
      1. Associated Types (or How Iterators Work)
      2. Generic Traits (or How Operator Overloading Works)
      3. impl Trait
      4. Associated Consts
    5. Reverse-Engineering Bounds
    6. Traits as a Foundation
  13. 12. Operator Overloading
    1. Arithmetic and Bitwise Operators
      1. Unary Operators
      2. Binary Operators
      3. Compound Assignment Operators
    2. Equivalence Comparisons
    3. Ordered Comparisons
    4. Index and IndexMut
    5. Other Operators
  14. 13. Utility Traits
    1. Drop
    2. Sized
    3. Clone
    4. Copy
    5. Deref and DerefMut
    6. Default
    7. AsRef and AsMut
    8. Borrow and BorrowMut
    9. From and Into
    10. TryFrom and TryInto
    11. ToOwned
    12. Borrow and ToOwned at Work: The Humble Cow
  15. 14. Closures
    1. Capturing Variables
      1. Closures That Borrow
      2. Closures That Steal
    2. Function and Closure Types
    3. Closure Performance
    4. Closures and Safety
      1. Closures That Kill
      2. FnOnce
      3. FnMut
      4. Copy and Clone for Closures
    5. Callbacks
    6. Using Closures Effectively
  16. 15. Iterators
    1. The Iterator and IntoIterator Traits
    2. Creating Iterators
      1. iter and iter_mut Methods
      2. IntoIterator Implementations
      3. from_fn and successors
      4. drain Methods
      5. Other Iterator Sources
    3. Iterator Adapters
      1. map and filter
      2. filter_map and flat_map
      3. flatten
      4. take and take_while
      5. skip and skip_while
      6. peekable
      7. fuse
      8. Reversible Iterators and rev
      9. inspect
      10. chain
      11. enumerate
      12. zip
      13. by_ref
      14. cloned, copied
      15. cycle
    4. Consuming Iterators
      1. Simple Accumulation: count, sum, product
      2. max, min
      3. max_by, min_by
      4. max_by_key, min_by_key
      5. Comparing Item Sequences
      6. any and all
      7. position, rposition, and ExactSizeIterator
      8. fold and rfold
      9. try_fold and try_rfold
      10. nth, nth_back
      11. last
      12. find, rfind, and find_map
      13. Building Collections: collect and FromIterator
      14. The Extend Trait
      15. partition
      16. for_each and try_for_each
    5. Implementing Your Own Iterators
  17. 16. Collections
    1. Overview
    2. Vec<T>
      1. Accessing Elements
      2. Iteration
      3. Growing and Shrinking Vectors
      4. Joining
      5. Splitting
      6. Swapping
      7. Filling
      8. Sorting and Searching
      9. Comparing Slices
      10. Random Elements
      11. Rust Rules Out Invalidation Errors
    3. VecDeque<T>
    4. BinaryHeap<T>
    5. HashMap<K, V> and BTreeMap<K, V>
      1. Entries
      2. Map Iteration
    6. HashSet<T> and BTreeSet<T>
      1. Set Iteration
      2. When Equal Values Are Different
      3. Whole-Set Operations
    7. Hashing
    8. Using a Custom Hashing Algorithm
    9. Beyond the Standard Collections
  18. 17. Strings and Text
    1. Some Unicode Background
      1. ASCII, Latin-1, and Unicode
      2. UTF-8
      3. Text Directionality
    2. Characters (char)
      1. Classifying Characters
      2. Handling Digits
      3. Case Conversion for Characters
      4. Conversions to and from Integers
    3. String and str
      1. Creating String Values
      2. Simple Inspection
      3. Appending and Inserting Text
      4. Removing and Replacing Text
      5. Conventions for Searching and Iterating
      6. Patterns for Searching Text
      7. Searching and Replacing
      8. Iterating over Text
      9. Trimming
      10. Case Conversion for Strings
      11. Parsing Other Types from Strings
      12. Converting Other Types to Strings
      13. Borrowing as Other Text-Like Types
      14. Accessing Text as UTF-8
      15. Producing Text from UTF-8 Data
      16. Putting Off Allocation
      17. Strings as Generic Collections
    4. Formatting Values
      1. Formatting Text Values
      2. Formatting Numbers
      3. Formatting Other Types
      4. Formatting Values for Debugging
      5. Formatting Pointers for Debugging
      6. Referring to Arguments by Index or Name
      7. Dynamic Widths and Precisions
      8. Formatting Your Own Types
      9. Using the Formatting Language in Your Own Code
    5. Regular Expressions
      1. Basic Regex Use
      2. Building Regex Values Lazily
    6. Normalization
      1. Normalization Forms
      2. The unicode-normalization Crate
  19. 18. Input and Output
    1. Readers and Writers
      1. Readers
      2. Buffered Readers
      3. Reading Lines
      4. Collecting Lines
      5. Writers
      6. Files
      7. Seeking
      8. Other Reader and Writer Types
      9. Binary Data, Compression, and Serialization
    2. Files and Directories
      1. OsStr and Path
      2. Path and PathBuf Methods
      3. Filesystem Access Functions
      4. Reading Directories
      5. Platform-Specific Features
    3. Networking
  20. 19. Concurrency
    1. Fork-Join Parallelism
      1. spawn and join
      2. Error Handling Across Threads
      3. Sharing Immutable Data Across Threads
      4. Rayon
      5. Revisiting the Mandelbrot Set
    2. Channels
      1. Sending Values
      2. Receiving Values
      3. Running the Pipeline
      4. Channel Features and Performance
      5. Thread Safety: Send and Sync
      6. Piping Almost Any Iterator to a Channel
      7. Beyond Pipelines
    3. Shared Mutable State
      1. What Is a Mutex?
      2. Mutex<T>
      3. mut and Mutex
      4. Why Mutexes Are Not Always a Good Idea
      5. Deadlock
      6. Poisoned Mutexes
      7. Multiconsumer Channels Using Mutexes
      8. Read/Write Locks (RwLock<T>)
      9. Condition Variables (Condvar)
      10. Atomics
      11. Global Variables
    4. What Hacking Concurrent Code in Rust Is Like
  21. 20. Asynchronous Programming
    1. From Synchronous to Asynchronous
      1. Futures
      2. Async Functions and Await Expressions
      3. Calling Async Functions from Synchronous Code: block_on
      4. Spawning Async Tasks
      5. Async Blocks
      6. Building Async Functions from Async Blocks
      7. Spawning Async Tasks on a Thread Pool
      8. But Does Your Future Implement Send?
      9. Long Running Computations: yield_now and spawn_blocking
      10. Comparing Asynchronous Designs
      11. A Real Asynchronous HTTP Client
    2. An Asynchronous Client and Server
      1. Error and Result Types
      2. The Protocol
      3. Taking User Input: Asynchronous Streams
      4. Sending Packets
      5. Receiving Packets: More Asynchronous Streams
      6. The Client’s Main Function
      7. The Server’s Main Function
      8. Handling Chat Connections: Async Mutexes
      9. The Group Table: Synchronous Mutexes
      10. Chat Groups: tokio’s Broadcast Channels
    3. Primitive Futures and Executors: When Is a Future Worth Polling Again?
      1. Invoking Wakers: spawn_blocking
      2. Implementing block_on
    4. Pinning
      1. The Two Life Stages of a Future
      2. Pinned Pointers
      3. The Unpin Trait
    5. When Is Asynchronous Code Helpful?
  22. 21. Macros
    1. Macro Basics
      1. Basics of Macro Expansion
      2. Unintended Consequences
      3. Repetition
    2. Built-In Macros
    3. Debugging Macros
    4. Building the json! Macro
      1. Fragment Types
      2. Recursion in Macros
      3. Using Traits with Macros
      4. Scoping and Hygiene
      5. Importing and Exporting Macros
    5. Avoiding Syntax Errors During Matching
    6. Beyond macro_rules!
  23. 22. Unsafe Code
    1. Unsafe from What?
    2. Unsafe Blocks
    3. Example: An Efficient ASCII String Type
    4. Unsafe Functions
    5. Unsafe Block or Unsafe Function?
    6. Undefined Behavior
    7. Unsafe Traits
    8. Raw Pointers
      1. Dereferencing Raw Pointers Safely
      2. Example: RefWithFlag
      3. Nullable Pointers
      4. Type Sizes and Alignments
      5. Pointer Arithmetic
      6. Moving into and out of Memory
      7. Example: GapBuffer
      8. Panic Safety in Unsafe Code
    9. Reinterpreting Memory with Unions
    10. Matching Unions
    11. Borrowing Unions
  24. 23. Foreign Functions
    1. Finding Common Data Representations
    2. Declaring Foreign Functions and Variables
    3. Using Functions from Libraries
    4. A Raw Interface to libgit2
    5. A Safe Interface to libgit2
    6. Conclusion
  25. Index
  26. About the Authors

Product information

  • Title: Programming Rust, 2nd Edition
  • Author(s): Jim Blandy, Jason Orendorff, Leonora F. S. Tindall
  • Release date: June 2021
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781492052593