Effective Rust

Book description

Rust's popularity is growing, due in part to features like memory safety, type safety, and thread safety. But these same elements can also make learning Rust a challenge, even for experienced programmers. This practical guide helps you make the transition to writing idiomatic Rust—while also making full use of Rust's type system, safety guarantees, and burgeoning ecosystem.

If you're a software engineer who has experience with an existing compiled language, or if you've struggled to convert a basic understanding of Rust syntax into working programs, this book is for you. By focusing on the conceptual differences between Rust and other compiled languages, and by providing specific recommendations that programmers can easily follow, Effective Rust will soon have you writing fluent Rust, not just badly translated C++.

  • Understand the structure of Rust's type system
  • Learn Rust idioms for error handling, iteration, and more
  • Discover how to work with Rust's crate ecosystem
  • Use Rust's type system to express your design
  • Win fights with the borrow checker
  • Build a robust project that takes full advantage of the Rust tooling ecosystem

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. Who This Book Is For
    2. Rust Version
    3. Navigating This Book
    4. Conventions Used in This Book
    5. O’Reilly Online Learning
    6. How to Contact Us
    7. Acknowledgments
  2. 1. Types
    1. Item 1: Use the type system to express your data structures
      1. Fundamental Types
      2. Aggregate Types
      3. enums
      4. enums with Fields
      5. Ubiquitous enum Types
    2. Item 2: Use the type system to express common behavior
      1. Functions and Methods
      2. Function Pointers
      3. Closures
      4. Traits
    3. Item 3: Prefer Option and Result transforms over explicit match expressions
      1. Things to Remember
    4. Item 4: Prefer idiomatic Error types
      1. The Error Trait
      2. Minimal Errors
      3. Nested Errors
      4. Trait Objects
      5. Libraries Versus Applications
      6. Things to Remember
    5. Item 5: Understand type conversions
      1. User-Defined Type Conversions
      2. Casts
      3. Coercion
    6. Item 6: Embrace the newtype pattern
      1. Bypassing the Orphan Rule for Traits
      2. Newtype Limitations
    7. Item 7: Use builders for complex types
    8. Item 8: Familiarize yourself with reference and pointer types
      1. Rust References
      2. Pointer Traits
      3. Fat Pointer Types
      4. More Pointer Traits
      5. Smart Pointer Types
    9. Item 9: Consider using iterator transforms instead of explicit loops
      1. Iterator Traits
      2. Iterator Transforms
      3. Iterator Consumers
      4. Building Collections from Result Values
      5. Loop Transformation
      6. When Explicit Is Better
  3. 2. Traits
    1. Item 10: Familiarize yourself with standard traits
      1. Common Standard Traits
      2. Standard Traits Covered Elsewhere
      3. Operator Overloads
      4. Summary
    2. Item 11: Implement the Drop trait for RAII patterns
    3. Item 12: Understand the trade-offs between generics and trait objects
      1. Generics
      2. Trait Objects
      3. Basic Comparisons
      4. More Trait Bounds
      5. Trait Object Safety
      6. Trade-Offs
    4. Item 13: Use default implementations to minimize required trait methods
  4. 3. Concepts
    1. Item 14: Understand lifetimes
      1. Introduction to the Stack
      2. Evolution of Lifetimes
      3. Scope of a Lifetime
      4. Algebra of Lifetimes
      5. Lifetime Elision Rules
      6. The 'static Lifetime
      7. Lifetimes and the Heap
      8. Lifetimes in Data Structures
      9. Anonymous Lifetimes
      10. Things to Remember
    2. Item 15: Understand the borrow checker
      1. Access Control
      2. Borrow Rules
      3. Owner Operations
      4. Winning Fights Against the Borrow Checker
      5. Things to Remember
    3. Item 16: Avoid writing unsafe code
    4. Item 17: Be wary of shared-state parallelism
      1. Data Races
      2. Deadlocks
      3. Advice
    5. Item 18: Don’t panic
    6. Item 19: Avoid reflection
      1. Upcasting in Future Versions of Rust
    7. Item 20: Avoid the temptation to over-optimize
      1. Data Structures and Allocation
      2. Who’s Afraid of the Big Bad Copy?
      3. References and Smart Pointers
  5. 4. Dependencies
    1. Item 21: Understand what semantic versioning promises
      1. Semver Essentials
      2. Semver for Crate Authors
      3. Semver for Crate Users
      4. Discussion
    2. Item 22: Minimize visibility
      1. Visibility Syntax
      2. Visibility Semantics
    3. Item 23: Avoid wildcard imports
    4. Item 24: Re-export dependencies whose types appear in your API
    5. Item 25: Manage your dependency graph
      1. Version Specification
      2. Solving Problems with Tooling
      3. What to Depend On
      4. Things to Remember
    6. Item 26: Be wary of feature creep
      1. Conditional Compilation
      2. Features
      3. Things to Remember
  6. 5. Tooling
    1. Item 27: Document public interfaces
      1. Tooling
      2. Additional Documentation Locations
      3. Published Crate Documentation
      4. What Not to Document
      5. Things to Remember
    2. Item 28: Use macros judiciously
      1. Declarative Macros
      2. Procedural Macros
      3. When to Use Macros
      4. Disadvantages of Macros
      5. Advice
    3. Item 29: Listen to Clippy
    4. Item 30: Write more than unit tests
      1. Unit Tests
      2. Integration Tests
      3. Doc Tests
      4. Examples
      5. Benchmarks
      6. Fuzz Testing
      7. Testing Advice
      8. Things to Remember
    5. Item 31: Take advantage of the tooling ecosystem
      1. Tools to Remember
    6. Item 32: Set up a continuous integration (CI) system
      1. CI Steps
      2. CI Principles
      3. Public CI Systems
  7. 6. Beyond Standard Rust
    1. Item 33: Consider making library code no_std compatible
      1. core
      2. alloc
      3. Writing Code for no_std
      4. Fallible Allocation
      5. Things to Remember
    2. Item 34: Control what crosses FFI boundaries
      1. Invoking C Functions from Rust
      2. Accessing C Data from Rust
      3. Lifetimes
      4. Invoking Rust from C
      5. Things to Remember
    3. Item 35: Prefer bindgen to manual FFI mappings
      1. Beyond C
  8. Afterword
  9. Index
  10. About the Author

Product information

  • Title: Effective Rust
  • Author(s): David Drysdale
  • Release date: April 2024
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781098151409