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 →
Also covered in
Related concepts
- declarative macroconfigured by · A Rust declarative macro is defined by rules that match input token sequences and expand them into generated code.
- gensymhas part · gensym is a Clojure function that generates a unique symbol, typically for avoiding name collisions in macro-generated 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.
- macrocontrasts with · A macro is a named, reusable construct that expands into generated code or a sequence of commands when invoked, often during preprocessing, compilation, or application execution.
Questions about macro hygiene
What is macro hygiene?
Macro hygiene is a property of macro expansion that prevents introduced or referenced identifiers from unintentionally conflicting with surrounding code.
Which Manning books explain macro hygiene?
Clojure: The Essential Reference and Write Powerful Rust Macros.
What is the difference between macro hygiene and macro?
Macro hygiene is a property of macro expansion that prevents introduced or referenced identifiers from unintentionally conflicting with surrounding code. A macro is a named, reusable construct that expands into generated code or a sequence of commands when invoked, often during preprocessing, compilation, or application execution.
What are the parts of macro hygiene?
Macro hygiene includes gensym.
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.

