Share E-Book
Scan to open this page

Scan with your phone to open this page

Author: Evan Williams, Scott McKay

Rating No ratings yet

No description

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

AI guide
# Design Patterns and Best Practices in Rust ## 【One-Line Pitch】 A practical guide for experienced Rust developers who want to move beyond basic syntax and learn how to structure real-world applications using idiomatic, Rust-native design patterns instead of forcing object-oriented habits from Java, C++, or Python onto a language that thinks differently. ## 【Book Arc】 - **Opening (~0%–12%)**: Introduces why Rust demands different design thinking than traditional OO languages, using a deliberately flawed "Bad Calculator" project to demonstrate how familiar patterns from Java/C++ fail when they hit Rust's ownership model, borrow checker, and trait system. - **Early (~12%–34%)**: Walks through three major anti-patterns—designing for object orientation (structs as classes, traits as interfaces, Deref abuse), overusing Clone and Rc/RefCell to dodge ownership, and fighting the borrow checker with unsafe code or mutable statics—showing why each approach creates more problems than it solves. - **Middle (~39%–49%)**: Rebuilds the calculator "correctly" using classic creational patterns (Factory Method, Abstract Factory, Builder) and behavioral patterns (Strategy, Mediator, Template Method, Iterator, State, Memento, Observer), demonstrating how traditional GoF patterns translate into idiomatic Rust. - **Middle (~49%–61%)**: Introduces patterns unique to Rust's type system—NewType wrappers, Parse Don't Validate, TypeState, Sealed Traits—plus functional programming patterns using closures, generics as type classes, and advanced pattern matching. - **Late (~61%–71%)**: Covers patterns emerging from Rust's core features: Result/Option composition, block expressions, RAII via Drop, and error-handling patterns. Concludes with a third project, "Samsa," a publish/subscribe microservice that leverages Rust's type system for compile-time safety and functional idioms for architecture. ## 【Key Takeaways】 - **Familiar patterns actively mislead in Rust** (Early): Experience with OO languages creates expectations—structs as classes, traits as interfaces, inheritance hierarchies—that the borrow checker and ownership model will reject. The "Bad Calculator" project demonstrates how these habits produce brittle, unidiomatic code. - **Clone and Rc are crutches, not solutions** (Early): Reaching for `clone()` or `Rc<RefCell<T>>` as a default sidesteps ownership problems but reintroduces the very bugs Rust's ownership model prevents—slow code, fragile reasoning, and runtime panics where compile-time guarantees should exist. - **The borrow checker is a design consultant, not an adversary** (Early): Fighting it with `unsafe`, mutable statics, or lifetime gymnastics creates more problems than it solves. Listening to its feedback and redesigning data structures so ownership flows naturally leads to better architecture. - **Classic GoF patterns translate, but with Rust's flavor** (Middle): Creational patterns like Factory and Builder work well in Rust, but Abstract Factory requires careful trait design; behavioral patterns like Strategy and Mediator map naturally onto traits and composition rather than inheritance. - **Rust's type system is an active design tool** (Middle): NewType wrappers add semantic meaning at zero runtime cost; Parse Don't Validate encodes data validity in types; TypeState makes illegal state transitions impossible at compile time; Sealed Traits restrict implementation to controlled type sets. - **Functional patterns are first-class architectural components** (Middle): Closures with capture semantics, function composition, pipelines, and generics as type classes are not just conveniences—they're practical design tools for building configurable, composable systems. - **Patterns emerge from Rust's core features** (Late): Result and Option composition, block expressions as scoping tools, RAII through Drop, and error-collection patterns show that the most idiomatic designs often arise naturally from the language's own mechanisms rather than imported traditions. - **Thinking in Rust is a mental shift** (Late): Across all three projects, the lesson is consistent—ownership as design philosophy, zero-cost abstractions as mindset, safety without paranoia, composition over inheritance, and explicitness as a core value. ## 【Reading Tips】 - **Deep-read Chapters 1–4** if you're coming from Java/C++/Python: these anti-pattern chapters are where the book earns its keep, showing exactly why your instincts will betray you and how to recognize those traps in your own code. - **Skim Chapter 5's creational patterns** if you already know Factory/Builder from other languages; focus instead on the "when and when not" discussions and the Rust-specific trait designs. - **Pay special attention to Chapters 10–12** (type-system patterns, functional patterns, core-feature patterns): these are the most Rust-specific material and where the book moves beyond translating GoF patterns into genuinely new territory. - **Work through the calculator projects** rather than just reading them—the book's pedagogy depends on seeing the "wrong" way fail before the "right" way succeeds; the accompanying repository includes test suites worth studying. - **Expect to be challenged on ownership**: if you're not yet comfortable with borrowing, lifetimes, and traits, review those fundamentals first—the book assumes working knowledge and focuses on design, not syntax. ## 【Coverage Limits】 This guide covers the book's structure, core arguments, and pattern taxonomy based on the table of contents, preface, and chapter summaries. Detailed code examples, specific implementation walkthroughs, and the Samsa microservice project's full design are not covered in this guide. ##
Excerpt 1
Birmingham B3 1RB, UK. ISBN 978-1-83620-947-8 www.packtpub.com To my parents, Howard & Rosalie Williams, Whose voices echo in my own – Evan Williams Foreword...
View in text
Excerpt 2
• Builder Why use Builder? • Why use a specialized builder?
View in text
Excerpt 3
der Why use Builder? • Why use a specialized builder?
View in text
Excerpt 4
eels like a different, and sometimes delightful, experience. You've probably also had some not-at-all fun moments. Code that would be straightforward in Java...
View in text
Excerpt 5
ncepts will help you appreciate how Rust's approach differs. Software/hardware covered in the book Operating system requirements Rust 1.78.0 or later Windows...
View in text
Excerpt 6
a twist so that they can be adapted to Rust's unique model. The reason that the familiar design patterns work so perfectly in so many situations is that popu...
View in text
Excerpt 7
they work, but this causes even more things to not compile. Taking a step back, they recognize that Rust doesn't permit them to do what they're trying to do,...
View in text
Excerpt 8
as they begin to feel truly comfortable using the language. That setback, that feeling of falling backward in the journey, can be so upsetting that some stop...
View in text
Tags
AI categories
Programming LanguageRustBackend
Publisher: Packt Publishing
Publish Year: 2026
Language: English
Pages: 440
File Format: EPUB
File Size: 1.1 MB