Programming Rust, 2nd Edition

Book description

The Rust programming language offers the rare and valuable combination of statically verified memory safety and low-level control. Imagine C++ but without dangling pointers, null pointer dereferences, or buffer overruns, and with a deep library of freely reusable libraries. This practical guide gives systems and application programmers alike a solid understanding of Rust’s rules, and shows how to put them to work ensuring their programs are not only efficient, but free of broad classes of common errors.

Rust brings the benefits of an expressive modern type system to systems programming. Authors Jim Blandy, Jason Orendorff, and Leonora F.S. Tindall demonstrate how Rust’s features put programmers in control over memory consumption and processor use, combining predictable performance with memory safety and trustworthy concurrency.

You’ll learn:

  • How to write fast, safe, concurrent programs in Rust
  • Rust’s rules for managing memory efficiently, including ownership, borrowing, moves, and lifetimes
  • How to design interfaces that fit well into the Rust ecosystem
  • Cargo, Rust’s all-purpose tool for building, testing, and managing Rust packages
  • High-level features like traits, generics, closures, and iterators that make Rust productive and flexible

Publisher resources

View/Submit Errata

Table of contents

  1. 1. Why Rust?
    1. Type Safety
  2. 2. Basic Types
    1. Machine Types
      1. Integer Types
      2. Checked, Wrapping, and Saturating Arithmetic
      3. Floating-Point Types
      4. The bool Type
      5. Characters
    2. Tuples
    3. Pointer Types
      1. References
      2. Boxes
      3. Raw Pointers
    4. Arrays, Vectors, and Slices
      1. Arrays
      2. Vectors
      3. Slices
    5. String Types
      1. String Literals
      2. Byte Strings
      3. Strings in Memory
      4. String
      5. Using Strings
      6. Other String-Like Types
    6. Beyond the Basics
  3. 3. Ownership
    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
  4. 4. References
    1. References as Values
      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
    2. Reference Safety
      1. Borrowing a Local Variable
      2. Receiving References as Parameters
      3. Passing References as Arguments
      4. Returning References
      5. Structs Containing References
      6. Distinct Lifetime Parameters
      7. Omitting Lifetime Parameters
    3. Sharing Versus Mutation
    4. Taking Arms Against a Sea of Objects
  5. 5. Expressions
    1. An Expression Language
    2. Blocks and Semicolons
    3. Declarations
    4. if and match
      1. if let
    5. Loops
    6. return Expressions
    7. Why Rust Has loop
    8. Function and Method Calls
    9. Fields and Elements
    10. Reference Operators
    11. Arithmetic, Bitwise, Comparison, and Logical Operators
    12. Assignment
    13. Type Casts
    14. Closures
    15. Precedence and Associativity
    16. Onward
  6. 6. 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?
  7. 7. Crates and Modules
    1. Crates
      1. Build Profiles
      2. Editions
    2. Modules
      1. Modules in Separate Files
      2. Paths and Imports
      3. The Standard Prelude
      4. Items, the Building Blocks of Rust
    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
  8. 8. 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. Static Methods
    6. Static Values
    7. Generic Structs
    8. Structs with Lifetime Parameters
    9. Deriving Common Traits for Struct Types
    10. Interior Mutability
  9. 9. 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. Matching Multiple Possibilities
      6. Pattern Guards
      7. @ Patterns
      8. Where Patterns Are Allowed
      9. Populating a Binary Tree
    3. The Big Picture
  10. 10. Traits and Generics
    1. Using Traits
      1. Trait Objects
      2. Trait Object Layout
      3. Generic Functions
      4. 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. Static Methods
    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. Conclusion
  11. 11. Operator Overloading
    1. Arithmetic and Bitwise Operators
      1. Unary Operators
      2. Binary Operators
      3. Compound Assignment Operators
    2. Equality Tests
    3. Ordered Comparisons
    4. Index and IndexMut
    5. Other Operators
  12. 12. 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. ToOwned
    11. Borrow and ToOwned at Work: The Humble Cow
  13. 13. 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
  14. 14. 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

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