Rust for Rustaceans

Book description

For developers who’ve mastered the basics, this book is the next step on your way to professional-level programming in Rust. It covers everything you need to build and maintain larger code bases, write powerful and flexible applications and libraries, and confidently expand the scope and complexity of your projects.

Author Jon Gjengset takes you deep into the Rust programming language, dissecting core topics like ownership, traits, concurrency, and unsafe code. You’ll explore key concepts like type layout and trait coherence, delve into the inner workings of concurrent programming and asynchrony with async/await, and take a tour of the world of no_std programming. Gjengset also provides expert guidance on API design, testing strategies, and error handling, and will help develop your understanding of foreign function interfaces, object safety, procedural macros, and much more.

You'll learn:

•How to design reliable, idiomatic, and ergonomic Rust programs based on best principles
•Effective use of declarative and procedural macros, and the difference between them
•How asynchrony works in Rust – all the way from the Pin and Waker types used in manual implementations of Futures, to how async/await saves you from thinking about most of those words
•What it means for code to be unsafe, and best practices for writing and interacting with unsafe functions and traits
•How to organize and configure more complex Rust projects so that they integrate nicely with the rest of the ecosystem
•How to write Rust code that can interoperate with non-Rust libraries and systems, or run in constrained and embedded environments

Brimming with practical, pragmatic insights that you can immediately apply, Rust for Rustaceans helps you do more with Rust, while also teaching you its underlying mechanisms.

Table of contents

  1. Title Page
  2. Copyright
  3. About the Author
  4. Foreword
  5. Preface
  6. Acknowledgments
  7. Introduction
  8. Chapter 1: Foundations
    1. Talking About Memory
      1. Memory Terminology
      2. Variables in Depth
      3. Memory Regions
    2. Ownership
    3. Borrowing and Lifetimes
      1. Shared References
      2. Mutable References
      3. Interior Mutability
      4. Lifetimes
    4. Summary
  9. Chapter 2: Types
    1. Types in Memory
      1. Alignment
      2. Layout
      3. Complex Types
      4. Dynamically Sized Types and Wide Pointers
    2. Traits and Trait Bounds
      1. Compilation and Dispatch
      2. Generic Traits
      3. Coherence and the Orphan Rule
      4. Trait Bounds
      5. Marker Traits
    3. Existential Types
    4. Summary
  10. Chapter 3: Designing Interfaces
    1. Unsurprising
      1. Naming Practices
      2. Common Traits for Types
      3. Ergonomic Trait Implementations
      4. Wrapper Types
    2. Flexible
      1. Generic Arguments
      2. Object Safety
      3. Borrowed vs. Owned
      4. Fallible and Blocking Destructors
    3. Obvious
      1. Documentation
      2. Type System Guidance
    4. Constrained
      1. Type Modifications
      2. Trait Implementations
      3. Hidden Contracts
    5. Summary
  11. Chapter 4: Error Handling
    1. Representing Errors
      1. Enumeration
      2. Opaque Errors
      3. Special Error Cases
    2. Propagating Errors
    3. Summary
  12. Chapter 5: Project Structure
    1. Features
      1. Defining and Including Features
      2. Using Features in Your Crate
    2. Workspaces
    3. Project Configuration
      1. Crate Metadata
      2. Build Configuration
    4. Conditional Compilation
    5. Versioning
      1. Minimum Supported Rust Version
      2. Minimal Dependency Versions
      3. Changelogs
      4. Unreleased Versions
    6. Summary
  13. Chapter 6: Testing
    1. Rust Testing Mechanisms
      1. The Test Harness
      2. #[cfg(test)]
      3. Doctests
    2. Additional Testing Tools
      1. Linting
      2. Test Generation
      3. Test Augmentation
      4. Performance Testing
    3. Summary
  14. Chapter 7: Macros
    1. Declarative Macros
      1. When to Use Them
      2. How They Work
      3. How to Write Declarative Macros
    2. Procedural Macros
      1. Types of Procedural Macros
      2. The Cost of Procedural Macros
      3. So You Think You Want a Macro
      4. How Do They Work?
    3. Summary
  15. Chapter 8: Asynchronous Programming
    1. What’s the Deal with Asynchrony?
      1. Synchronous Interfaces
      2. Multithreading
      3. Asynchronous Interfaces
      4. Standardized Polling
    2. Ergonomic Futures
      1. async/await
      2. Pin and Unpin
    3. Going to Sleep
      1. Waking Up
      2. Fulfilling the Poll Contract
      3. Waking Is a Misnomer
      4. Tasks and Subexecutors
    4. Tying It All Together with spawn
    5. Summary
  16. Chapter 9: Unsafe Code
    1. The unsafe Keyword
    2. Great Power
      1. Juggling Raw Pointers
      2. Calling Unsafe Functions
      3. Implementing Unsafe Traits
    3. Great Responsibility
      1. What Can Go Wrong?
      2. Validity
      3. Panics
      4. Casting
      5. The Drop Check
    4. Coping with Fear
      1. Manage Unsafe Boundaries
      2. Read and Write Documentation
      3. Check Your Work
    5. Summary
  17. Chapter 10: Concurrency (and Parallelism)
    1. The Trouble with Concurrency
      1. Correctness
      2. Performance
    2. Concurrency Models
      1. Shared Memory
      2. Worker Pools
      3. Actors
    3. Asynchrony and Parallelism
    4. Lower-Level Concurrency
      1. Memory Operations
      2. Atomic Types
      3. Memory Ordering
      4. Compare and Exchange
      5. The Fetch Methods
    5. Sane Concurrency
      1. Start Simple
      2. Write Stress Tests
      3. Use Concurrency Testing Tools
    6. Summary
  18. Chapter 11: Foreign Function Interfaces
    1. Crossing Boundaries with extern
      1. Symbols
      2. Calling Conventions
    2. Types Across Language Boundaries
      1. Type Matching
      2. Allocations
      3. Callbacks
      4. Safety
    3. bindgen and Build Scripts
    4. Summary
  19. Chapter 12: Rust Without the Standard Library
    1. Opting Out of the Standard Library
    2. Dynamic Memory Allocation
    3. The Rust Runtime
      1. The Panic Handler
      2. Program Initialization
      3. The Out-of-Memory Handler
    4. Low-Level Memory Accesses
    5. Misuse-Resistant Hardware Abstraction
    6. Cross-Compilation
    7. Summary
  20. Chapter 13: The Rust Ecosystem
    1. What’s Out There?
      1. Tools
      2. Libraries
      3. Rust Tooling
      4. The Standard Library
    2. Patterns in the Wild
      1. Index Pointers
      2. Drop Guards
      3. Extension Traits
      4. Crate Preludes
    3. Staying Up to Date
    4. What Next?
      1. Learn by Watching
      2. Learn by Doing
      3. Learn by Reading
      4. Learn by Teaching
    5. Summary
  21. Index

Product information

  • Title: Rust for Rustaceans
  • Author(s): Jon Gjengset
  • Release date: December 2021
  • Publisher(s): No Starch Press
  • ISBN: 9781718501850