Skip to product information
Rust Clean Code
Rust Clean Code
Description
Book Introduction
Digging deep into Rust to reveal its true core.

Rust Clean Code introduces Rust-like clean code and design patterns to help you get the most out of Rust's unique language design. It covers topics like metaprogramming, how to implement libraries yourself, and writing fluent interfaces, all with clear explanations and reusable code examples.
In this course, you will understand the inner workings of Rust and learn how to write efficient Rust code that is easy to maintain and continuously evolve.
  • You can preview some of the book's contents.
    Preview

index
Translator's Preface x
Beta Reader Review xi
Beginning xiii
Acknowledgements xiv
About this book xv
About the cover xix

PART I COMPONENTS

CHAPTER 1 Rustdown Pattern 3
1.1 Topics covered in this book 5
1.2 What is a design pattern? 6
1.3 Why This Book Is Different 9
1.4 Tools Required 10
1.5 Summary 11

CHAPTER 2: The Basic Building Blocks of Rust 13
2.1 Generic 14
2.1.1 Turing-complete type system 14
2.1.2 Why Generics? 15
2.1.3 Basics of Generics 15
2.1.4 A Look at Rust's Options 19
2.1.5 Marker Structure and Phantom Type 19
2.1.6 Generic Parameter Trait Bounds 23
2.2 Trait 23
2.2.1 Why Traits Are Not Object-Oriented Programming 24
2.2.2 What's in the Trait 25
2.2.3 Understanding Traits by Examining Object-Oriented Code 26
2.2.4 Combining Generics and Traits 30
2.2.5 Automatically Deriving Traits 35
2.2.6 Trait Object 37
2.3 Summary 40

CHAPTER 3 Code Flow 43
3.1 Pattern Matching Overview 44
3.1.1 Basics of Pattern Matching 44
3.1.2 Concise processing with the ? operator 50
3.2 Functional Rust 53
3.2.1 Rust Functional Programming Fundamentals 54
3.2.2 Capturing Closure Variables 56
3.2.3 Examining Iterators 57
3.2.4 Obtaining an iterator with iter(), into_iter(), and iter_mut() 62
3.2.5 Iterator Functions 71
3.3 Summary 76

PART II Core Patterns

CHAPTER 4 Basic Patterns 79
4.1 Acquiring resources is initialized to 80
4.1.1 RAII in C and C++ 81
4.1.2 A Tour of Rust's RAII 85
4.1.3 Summary of Rust's RAII 87
4.2 Passing arguments by value and by reference 89
4.2.1 Passing by Value 89
4.2.2 Passing by Reference 91
4.2.3 When to do what?: Passing by value and passing by reference 93
4.3 Constructor 94
4.4 Object Member Visibility and Accessibility 97
4.5 Error Handling 99
4.6 Global Status 102
4.6.1 lazy-static.rs 105 / 4.6.2 once_cell 106
4.6.3 static_init 107 / 4.6.4 std::cell::OnceCell 108
4.7 Summary 109

CHAPTER 5 Design Patterns: Beyond the Basics 111
5.1 Metaprogramming with Macros 112
5.1.1 Basic Declarative Macros 113
5.1.2 When should I use macros? 115
5.1.3 Writing a Mini-DSL Using Macros 121
5.1.4 Using Macros for DRY 122
5.2 Optional function arguments 126
5.2.1 Examining Optional Arguments in Python 126
5.2.2 Examining Optional Arguments in C++ 127
5.2.3 Rust has no optional arguments 127
5.2.4 Mocking Optional Arguments with Traits 128
5.3 Builder Pattern 131
5.3.1 Implementing the Builder Pattern 132
5.3.2 Strengthening Builders with Traits 134
5.3.3 Developing Builders with Macros 135
5.4 Fluent Interface Pattern 139
5.4.1 Fluent Builder 139
5.4.2 Fluent Builder Test 142
5.5 Observer Pattern 143
5.5.1 Why Not Use Callbacks? 143
5.5.2 Implementing an Observer 144
5.6 Command Pattern 147
5.6.1 Defining Command Patterns 148
5.6.2 Implementing the Command Pattern 149
5.7 Newtype Pattern 152
5.8 Summary 155

CHAPTER 6 Designing Libraries 157
6.1 Good Library Design 158
6.2 Do One Thing Right, Do It Well 159
6.3 Avoiding Excessive Abstraction 160
6.4 Sticking to Basic Types 160
6.5 Using Tools 161
6.6 Good artists copy, great artists steal (from the standard library) 162
6.7 Document everything and provide examples 162
6.8 Don't mess with your users' code 163
6.9 Think about the state 163
6.10 Considering Aesthetics 164
6.11 Ease of Use of Rust Libraries 165
6.11.1 Revisiting Linked Lists 165
6.11.2 Improving API Design with Rustdoc 167
6.11.3 Improving Linked Lists with More Testing 174
6.11.4 Creating a Library That's Easy for Others to Debug 176
6.12 Summary 179

PART III ADVANCED PATTERNS

CHAPTER 7: Using Traits, Generics, and Structs for Specialized Tasks 183
7.1 Constant Generics 184
7.2 Implementing Traits for External Crate Types 186
7.2.1 Wrapper Structures 187 / 7.2.2 Unwrapping Wrapped Structures Using Deref 187
7.3 Extended Trait 189
7.4 Blanket Trait 191
7.5 Marker Trait 194
7.6 Structure Tagging 196
7.7 Reference Objects 199
7.8 Summary 205

CHAPTER 8: State Machines, Coroutines, Macros, and Preludes 207
8.1 Trait State Machine 208
8.2 Coroutines 212
8.3 Procedural Macros 218
8.4 Prelude 222
8.5 Summary 226

PART IV Avoiding Problems
CHAPTER 9: IMMUTABILITY 229
9.1 The Advantages of Immutability 230
9.2 Why Immutability Isn't a Panacea 232
9.3 How to Think About Immutable Data 233
9.4 Understanding Rust's Immutability 234

9.5 Examining the Basic Concept of Immutability 236
9.6 Using Traits to Make (Almost) Everything Immutable 239
9.7 Using Cow for Immutability 240
9.8 Crate for Immutable Data Structures 244
9.8.1 Using im 244 / 9.8.2 Using rpds 245
9.9 Summary 247

CHAPTER 10: ANTIPATTERNS 249
10.1 What is an Antipattern? 250
10.2 Using unsafe 251
10.2.1 What does unsafe do? 253
10.2.2 Where can I use unsafe? 254
10.2.3 When should I use unsafe? 256
10.2.4 Is unsafe dangerous? 257
10.3 Using unwrap() 257
10.4 Not Using Vec 258
10.5 Too many copies 262
10.6 Using Deref to Emulate Polymorphism 263
10.7 Global Data and Singletons 268
10.8 Too Many Smart Pointers 269
10.9 The Way Forward 270
10.10 Summary 271

APPENDIX A Installing Rust 273
A.1 Installing the Tools Required for This Book 273
A.1.1 Installing Tools for macOS Using Homebrew 273
A.1.2 Installing Tools for Linux Systems 274
A.1.3 Installing Tools for Windows 274
A.2 Managing rustc and other Rust components with rustup 275
A.2.1 Installing rustc and other components 275
A.2.2 Switching the default toolchain with rustup 275
A.2.3 Updating Rust Components 275

Search 277

Detailed image
Detailed Image 1

Into the book
Unlike Design Patterns from the GoF, this book is not a list of design patterns, but rather a discussion and exploration of specific patterns, examples, and implementations.
There are two reasons why we don't list patterns:
Patterns are not just templates or boilerplate, and copying and pasting patterns only gets you 10% (or less) of the way to complete code.
This book is for readers who have a desire for knowledge and personal growth.

--- p.9

Rust's unique language features require more thought when designing APIs and writing high-quality code.
In particular, you need to think more about how to manage memory and object lifetimes, pass values ​​between contexts, avoid race conditions, and ensure that your API is user-friendly.
Rust is also full of new opportunities to create and discover new patterns, and it will undoubtedly continue to evolve even after this book is published.
To get to Mars, we need to build a rocket that can get to Mars, and we also need to solve countless problems that will arise during the seven-month journey.

--- p.10

Although we mentioned in the introduction to this chapter that newtypes are a Rust-specific pattern, strictly speaking, there are no restrictions on applying the same concept to other programming languages.
As far as I know, the term Rust-specific simply means that the pattern originated within the Rust community.

--- p.152

When asked how he created the statue of David, Michelangelo replied, “I just carved away everything that didn’t look like David.”
The same goes for library design.
We need to trim away unnecessary abstractions until we are left with the simplest and most elegant solution to the problem we are trying to solve.

--- p.160

Ultimately, the definition of an anti-pattern comes down to opinion and preference.
However, in some cases, a practice may be objectively bad, for example, if it is unsafe, inefficient, or difficult to maintain.
These cases are often the result of a combination of factors, including poor design, the desire to maintain backward compatibility, and ongoing changes to acceptable software design.
--- p.250

Publisher's Review
A deep dive into Rust.
Digging into the practical core of Rust, not its theory.

To truly unlock the power of Rust, which has a high barrier to entry, it's essential to go beyond simply learning the grammar and understand and utilize the language's unique design patterns and idioms.

If you've started using Rust in real-world projects, you're entering a whole new dimension of exploration.
How can we apply standard design patterns in Rust applications? Where and why should we use IntoIterator? Why do Rust developers love the PhantomData type? This book answers these and many other questions.

Rust Clean Code introduces Rust-like clean code and design patterns to help you get the most out of Rust's unique language design. It covers topics like metaprogramming, how to implement libraries yourself, and writing fluent interfaces, all with clear explanations and reusable code examples.
In this course, you will understand the inner workings of Rust and learn how to write efficient Rust code that is easy to maintain and continuously evolve.

■ Creating an API that is enjoyable to use
■ How to apply classic design patterns such as the builder pattern
■ Functional programming patterns
■ Rust Antipatterns
GOODS SPECIFICS
- Date of issue: September 4, 2025
- Page count, weight, size: 300 pages | 188*245*15mm
- ISBN13: 9791194587323

You may also like

카테고리