Preface
Introduction
- Who This Book Is For
- How to Use This Book
- What This Book Is Not
- A Note on Editions
- About the Author
Chapter 1: Getting Started
- Installing Rust
- Creating a New Project
- The Main Function
- println! — Rust’s puts
- String Interpolation with format!
- Functions
- Chapter Exercises
- Summary
Chapter 2: Variables, Types, and Mutability
- let — Variable Binding
- Type Inference and Explicit Types
- Scalar Types
- Constants
- Shadowing
- Compound Types: Tuples and Arrays
- Chapter Exercises
- Summary
Chapter 3: Structs, Methods, and impl Blocks
- Defining a Struct
- Creating Instances
- Accessing Fields
- Struct Update Syntax
- Tuple Structs
- Unit Structs
- impl Blocks — Adding Methods
- Associated Functions (Constructors)
- Multiple impl Blocks
- The Debug Trait
- Ruby Class vs Rust Struct: Side by Side
- Chapter Exercises
- Summary
Chapter 4: Enums and Pattern Matching
- Defining an Enum
- match — Rust’s case/when
- if let — Concise Single-Pattern Matching
- Option — The Billion-Dollar Null Replacement
- Result — Handling Fallible Operations
- Pattern Matching Power Tools
- Chapter Exercises
- Summary
Chapter 5: Ownership and Borrowing
- The Problem Ownership Solves
- The Three Ownership Rules
- Move Semantics
- Copy Types
- Clone — Explicit Deep Copy
- Ownership and Functions
- Borrowing and References
- The Borrowing Rules
- Dangling References
- Slices — References to a Portion
- Ownership in Structs
- Ruby vs Rust: Memory Management Side by Side
- Chapter Exercises
- Summary
Chapter 6: Collections and Iterators
- Vec — Rust’s Array
- HashMap — Rust’s Hash
- Iterators — Rust’s Enumerable
- Collecting Results
- Collecting into Result and Option
- Closures
- Real-World Iterator Example
- Chapter Exercises
- Summary
Chapter 7: Error Handling
- Result — Errors as Values
- The ? Operator — Propagate Errors Up
- unwrap and expect
- Handling Different Error Types
- panic! — When Recovery Is Impossible
- Error Handling: Ruby vs Rust
- Chapter Exercises
- Summary
Chapter 8: Traits and Generics
- Defining a Trait
- Default Trait Implementations
- Trait Bounds and impl Trait
- Generics — Type Parameters
- Derive Macros — Auto-Generating Trait Implementations
- Trait Objects — Dynamic Dispatch
- Common Standard Library Traits
- Traits vs Ruby Modules: Side by Side
- Chapter Exercises
- Summary
Chapter 9: Strings and Text
- String and &str — Owned vs Borrowed
- Converting Between String and &str
- String Concatenation and format!
- Indexing into Strings
- Common String Operations
- Parsing Strings into Numbers
- Building Strings Efficiently
- Bytes, Chars, and Grapheme Clusters
- Regular Expressions
- Raw String Literals
- Ruby vs Rust: String Operations Cheat Sheet
- Chapter Exercises
- Summary
Chapter 10: Lifetimes
- Why Lifetimes Exist
- Lifetime Annotation Syntax
- When the Compiler Needs Lifetime Annotations
- Lifetime Elision Rules
- Lifetimes in Structs
- Static Lifetime
- Lifetime Annotations in Practice
- Ruby vs Rust: Lifetimes are Not in Ruby
- Chapter Exercises
- Summary
Chapter 11: Concurrency
- Spawning Threads
- Message Passing with Channels
- Shared State with Arc and Mutex
- Send and Sync — The Safety Traits
- The Rayon Crate — Parallel Iterators
- Concurrency Patterns: Ruby vs Rust
- Choosing a Concurrency Approach
- Chapter Exercises
- Summary
Chapter 12: Modules, Crates, and Testing
- Packages and Crates
- Modules — Organizing Code with mod
- Visibility with pub
- use — Bringing Names into Scope
- External Crates and Cargo.toml
- Testing
- Common Project Layout
- Ruby vs Rust: Project Structure Side by Side
- Chapter Exercises
- Summary
Appendix A: The Rust Toolchain
- rustup — The Toolchain Manager
- Cargo Commands Reference
- Editor Setup
- Clippy — The Linter
- rustfmt — The Formatter
- Useful Crates by Category
- Rust Documentation
- CI Configuration (GitHub Actions)
- Next Steps After This Book
Appendix B: Exercise Answers
- Chapter 1
- Chapter 2
- Chapter 4
- Chapter 5
- Chapter 6
- Chapter 7
- Chapter 8
- Chapter 9
- Chapter 10
- Chapter 11
- Chapter 12