Macro hygiene
Macro hygiene is a property of macro expansion that prevents introduced or referenced identifiers from unintentionally conflicting with surrounding code.
Clojure: The Essential Reference
Macro hygiene is the practice of preventing names introduced by macro expansion from unintentionally colliding with names in the surrounding code.
- How it is used
- Prevents accidental symbol capture when macros construct code for a caller’s context.
- How deep it goes
- Explains syntax-quote qualification, auto-gensym, and an escape hatch through expanded examples, but does not implement a purely hygienic macro system.
- Examples
- Compares `foo`, `foo#`, and `~'foo`, then shows a generated `let` binding and generated names in a retry macro.
- What you can do after
- Apply it via Clojure syntax-quote and auto-gensym when introducing names in macros, and inspect expansions with `macroexpand`.
Read how Clojure: The Essential Reference explains macro hygiene →
Write Powerful Rust Macros
Macro hygiene is the property that prevents identifiers introduced or referenced by a macro expansion from unintentionally colliding with identifiers in surrounding code.
- How it is used
- Explains why declarative macros can generate local names without unintentionally capturing symbols from surrounding Rust code.
- How deep it goes
- Defines identifier separation, demonstrates its scope consequence, and stops short of broader macro-expansion internals.
- Examples
- Uses generate_x!(), a generated let binding, an attempted increment, and passing identifiers as macro arguments.
- What you can do after
- Diagnose unresolved macro-related identifiers and pass external names explicitly when generated code must affect surrounding code.
Read how Write Powerful Rust Macros explains macro hygiene →
Related concepts
- declarative macroconfigured by · macro_rules! is Rust syntax for defining declarative macros by matching input token patterns and expanding matching rules into generated source code.
- identifieruses · An identifier is a name or value that denotes or distinguishes an entity or program element within a system.
- syntax-quoteaddressed by · Syntax-quote is Clojure reader syntax that quotes a form while supporting symbol qualification, generated symbols, unquoting, and unquote-splicing.
- gensymhas part · gensym is a Clojure function that generates a unique symbol, typically for avoiding name collisions in macro-generated code.
- macrocontrasts with · A macro is a named construct that expands or transforms source code, commands, or templates before or during execution.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.

