Write Powerful Rust Macros
{"all_nodes":"PHASE 1 fill complete; deterministic lint clean (2026-10-12)"}local graph
Write Powerful Rust Macros — Sam Van Overmeire
Section titled “Write Powerful Rust Macros — Sam Van Overmeire”chapter_01
Section titled “chapter_01”node-book_write-powerful-rust-macros_chapter-01-preface — Preface
chapter_02
Section titled “chapter_02”node-book_write-powerful-rust-macros_chapter-02-going-meta — Going meta
chapter_03
Section titled “chapter_03”node-book_write-powerful-rust-macros_chapter-03-declarative-macros — Declarative macros
chapter_04
Section titled “chapter_04”node-book_write-powerful-rust-macros_chapter-04-a-hello-world-procedural-macro — A “Hello, World” procedural macro
chapter_05
Section titled “chapter_05”node-book_write-powerful-rust-macros_chapter-05-making-fields-public-with-attribute-macros — Making fields public with attribute macros
chapter_06
Section titled “chapter_06”node-book_write-powerful-rust-macros_chapter-06-hiding-information-and-creating-mini-dsls-with-function-like-macros — Hiding information and creating mini-DSLs with function-like macros
chapter_07
Section titled “chapter_07”node-book_write-powerful-rust-macros_chapter-07-testing-a-builder-macro — Testing a builder macro
chapter_08
Section titled “chapter_08”node-book_write-powerful-rust-macros_chapter-08-from-panic-to-result-error-handling — From panic to result: Error handling
chapter_09
Section titled “chapter_09”node-book_write-powerful-rust-macros_chapter-09-builder-with-attributes — Builder with attributes
chapter_10
Section titled “chapter_10”node-book_write-powerful-rust-macros_chapter-10-writing-an-infrastructure-dsl — Writing an infrastructure DSL
chapter_11
Section titled “chapter_11”node-book_write-powerful-rust-macros_chapter-11-macros-and-the-outside-world — Macros and the outside world
chapter_12
Section titled “chapter_12”node-book_write-powerful-rust-macros_chapter-12-appendix-exercise-solutions — Appendix: Exercise solutions
chapter_13
Section titled “chapter_13”node-book_write-powerful-rust-macros_chapter-13-index — Index
Title Page
Section titled “Title Page”Write Powerful Rust Macros
Section titled “Write Powerful Rust Macros”Sam Van Overmeire
MANNING
Table of Contents
Section titled “Table of Contents”contents
Section titled “contents”preface xi
acknowledgments xiii
about this book xiv
about the author xvii
about the cover illustration xviii
1 Going meta 1
Section titled “1 Going meta 1”1.1 A day in the life of a Rust developer 2
1.2 What is metaprogramming? 3
1.3 Metaprogramming in Rust 4
Macro galore 5 • Appropriate use cases 6 • Unfit for purpose: When not to use macros 10
1.4 Approach of this book 10
1.5 Exercise 11
2 Declarative macros 12
Section titled “2 Declarative macros 12”2.1 Creating vectors 13
Syntax basics 13 • Declaring and exporting declarative macros 14 • The first matcher explained 15 • Nonempty matchers 15
2.2 Use cases 19
Varargs and default arguments 19 • More than one way to expand code 20 • Newtypes 23 • DSLs 28 • Composing is
vi
CONTENTS
vii
easy 31 • Currying, on the other hand … 34 • Hygiene is something to consider as well 35
2.3 From the real world 36
2.4 Exercises 38
3 A “Hello, World” procedural macro 41
3.1 Basic setup of a procedural macro project 42
3.2 Analyzing the procedural macro setup 45
3.3 Generating output 48
3.4 Experimenting with our code 50
3.5 cargo expand 51
3.6 The same macro—without syn and quote 52
3.7 From the real world 54
3.8 Exercises 55
4 Making fields public with attribute macros 57
4.1 Setup of an attribute macro project 58
4.2 Attribute macros vs. derive macros 59
4.3 First steps in public visibility 60
4.4 Getting and using fields 61
4.5 Possible extensions 66
4.6 More than one way to parse a stream 67
Delegating tasks to a custom struct 67 • Implementing the Parse trait 69 • Going low, low, low with cursor 71
4.7 Even more ways to develop and debug 72
4.8 From the real world 73
4.9 Exercises 76
5 Hiding information and creating mini-DSLs with function-like macros 78
5.1 Hiding information 79
Setup of the information-hiding macro 79 • Recreating the struct 81 • Generating the helper methods 83
5.2 Debugging by writing normal code 88
5.3 Composing 89
contents
Section titled “contents”preface xi
acknowledgments xiii
about this book xiv
about the author xvii
about the cover illustration xviii
1 Going meta 1
Section titled “1 Going meta 1”1.1 A day in the life of a Rust developer 2
1.2 What is metaprogramming? 3
1.3 Metaprogramming in Rust 4
Macro galore 5 • Appropriate use cases 6 • Unfit for purpose: When not to use macros 10
1.4 Approach of this book 10
1.5 Exercise 11
2 Declarative macros 12
Section titled “2 Declarative macros 12”2.1 Creating vectors 13
Syntax basics 13 • Declaring and exporting declarative macros 14 • The first matcher explained 15 • Nonempty matchers 15
2.2 Use cases 19
Varargs and default arguments 19 • More than one way to expand code 20 • Newtypes 23 • DSLs 28 • Composing is
vi
CONTENTS
vii
easy 31 • Currying, on the other hand … 34 • Hygiene is something to consider as well 35
2.3 From the real world 36
2.4 Exercises 38
3 A “Hello, World” procedural macro 41
3.1 Basic setup of a procedural macro project 42
3.2 Analyzing the procedural macro setup 45
3.3 Generating output 48
3.4 Experimenting with our code 50
3.5 cargo expand 51
3.6 The same macro—without syn and quote 52
3.7 From the real world 54
3.8 Exercises 55
4 Making fields public with attribute macros 57
4.1 Setup of an attribute macro project 58
4.2 Attribute macros vs. derive macros 59
4.3 First steps in public visibility 60
4.4 Getting and using fields 61
4.5 Possible extensions 66
4.6 More than one way to parse a stream 67
Delegating tasks to a custom struct 67 • Implementing the Parse trait 69 • Going low, low, low with cursor 71
4.7 Even more ways to develop and debug 72
4.8 From the real world 73
4.9 Exercises 76
5 Hiding information and creating mini-DSLs with function-like macros 78
5.1 Hiding information 79
Setup of the information-hiding macro 79 • Recreating the struct 81 • Generating the helper methods 83
5.2 Debugging by writing normal code 88
5.3 Composing 89
viii
CONTENTS
5.4 Anything you can do, I can do better 93
5.5 From the real world 94
5.6 Exercises 96
6 Testing a builder macro 97
6.1 Builder macro project setup 99
6.2 Fleshing out the structure of our setup 101
6.3 Adding white-box unit tests 103
6.4 Black-box unit tests 105
A happy path test 105 • A happy path test with an actual property 107 • Testing enables refactoring 112 • Further improvements and testing 116 • An alternative approach 119
Unhappy path 122
6.5 What kinds of unit tests do I need? 123
6.6 Beyond unit tests 124
6.7 From the real world 126
6.8 Exercises 127
7 From panic to result: Error handling 128
7.1 Errors and control flow 129
7.2 Pure and impure functions 130
7.3 Alternatives to exceptions 132
7.4 Rust’s Result and panics 135
7.5 Setup of the panic project 137
7.6 Mutable or immutable returns 139
7.7 Getting results 140
7.8 Don’t panic 145
Changing the panic into a Result 145 • Debugging observations 148
7.9 Error-handling flavors 149
Using syn for error handling 150 • Using proc_macro_error for error handling 155 • Deciding between syn and proc_macro_error 159
7.10 From the real world 159
7.11 Exercises 162
CONTENTS
Section titled “CONTENTS”8 Builder with attributes 163
Section titled “8 Builder with attributes 163”- 8.1 A rename attribute 164
- Testing the new attribute 164 • Implementing the attribute’s behavior 165 • Parsing variations 168
- 8.2 Alternative naming for attributes 170
- 8.3 Sensible defaults 173
- 8.4 A better error message for defaults 177
- 8.5 Build back better 179
- Avoiding illegal states and the type state pattern 179
- Combining the builder pattern with type state 182
- 8.6 Avoiding scattered conditionals 190
- 8.7 Attribute tokens and attributes 192
- 8.8 Other attributes 197
- 8.9 From the real world 199
- 8.10 Exercises 201
9 Writing an infrastructure DSL 203
Section titled “9 Writing an infrastructure DSL 203”- 9.1 What is IaC? What is AWS? 204
- 9.2 How our DSL works 205
- 9.3 Parsing our input 207
- Project setup and usage examples 207 • Implementing the Parse trait for our structs 209
- 9.4 Two alternative parsing approaches 215
- Using Punctuated with a custom struct 215 • Using Punctuated with a custom enum and builder 218
- 9.5 Actually creating the services 221
- 9.6 The two AWS clients 225
- 9.7 Errors and declarative macros 229
- 9.8 The right kind of testing 231
- 9.9 From the real world 231
- 9.10 Exercises 234
10 Macros and the outside world 235
Section titled “10 Macros and the outside world 235”- 10.1 A function-like configuration macro 236
- Macro project structure 236 • Code overview 237
- Using full paths 240
x
10.2 Adding another macro 242
10.3 Features 243
10.4 Documenting a macro 248
10.5 Publishing our macro 250
10.6 From the real world 253
10.7 Where to go from here 258
10.8 Exercises 258
appendix Exercise solutions 259
index 291