Skip to content
ImagineWorld

Write Powerful Rust Macros

node_idnode-book_write-powerful-rust-macros
node_typenode-book
schema_profileworld.node_book.v1
statusactive
orthogonal_scopemanifold, entropy, authority, state
manifoldworld
entropymedium
authorityinforms
stateexternal
domainimagineworld.net
node_interiorrosetta_contract
unisonone evidence claim read five ways: seated in the sensory-ingest field, this book is self-perception in miniature — a compiler's front half folded back onto its own language
governed_byWORLD-MANIFOLD
constitutional_citationCONSTITUTION
evidence_bundle
compared_by
grounded_by
anchors_into
peer_node_books
pillar_membershipF — compiler_trinity
book_titleWrite Powerful Rust Macros
book_authorSam Van Overmeire
book_publisherManning
total_chapters13
total_source_pages311
total_nodes14
source_pdfwrite-powerful-rust-macros.pdf
source_pdf_sha25680aa6ba0ee7810c0306f0bc12f7df19a37b7650be67accdac51d7735444ad9f8
ocr_sourceLightOnOCR-3-4B via vLLM (400dpi; grounding+transcription modes)
ocr_quality_classcleaned
verification_statusone-shot-fill-lint-clean
verification_progress{"all_nodes":"PHASE 1 fill complete; deterministic lint clean (2026-10-12)"}
stitched_bykimi-k3-lightonocr3-one-shot
generated_at2026-10-12
pillar_normalizedF-compiler_trinity
updated_at2026-10-14T04:54:18Z
visual_conversions20
visual_conversion_placeholders0
asset_count16
local graph

Write Powerful Rust Macros — Sam Van Overmeire

Section titled “Write Powerful Rust Macros — Sam Van Overmeire”

node-book_write-powerful-rust-macros_chapter-01-preface — Preface

node-book_write-powerful-rust-macros_chapter-02-going-meta — Going meta

node-book_write-powerful-rust-macros_chapter-03-declarative-macros — Declarative macros

node-book_write-powerful-rust-macros_chapter-04-a-hello-world-procedural-macro — A “Hello, World” procedural macro

node-book_write-powerful-rust-macros_chapter-05-making-fields-public-with-attribute-macros — Making fields public with attribute macros

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

node-book_write-powerful-rust-macros_chapter-07-testing-a-builder-macro — Testing a builder macro

node-book_write-powerful-rust-macros_chapter-08-from-panic-to-result-error-handling — From panic to result: Error handling

node-book_write-powerful-rust-macros_chapter-09-builder-with-attributes — Builder with attributes

node-book_write-powerful-rust-macros_chapter-10-writing-an-infrastructure-dsl — Writing an infrastructure DSL

node-book_write-powerful-rust-macros_chapter-11-macros-and-the-outside-world — Macros and the outside world

node-book_write-powerful-rust-macros_chapter-12-appendix-exercise-solutions — Appendix: Exercise solutions

node-book_write-powerful-rust-macros_chapter-13-index — Index

Sam Van Overmeire

MANNING

preface xi
acknowledgments xiii
about this book xiv
about the author xvii
about the cover illustration xviii

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.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

preface xi
acknowledgments xiii
about this book xiv
about the author xvii
about the cover illustration xviii

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.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

  • 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.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.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