Macro expansion
Macro expansion is the compile-time transformation of a macro invocation into source tokens or language constructs according to its definition and expansion rules.
Modern C, Third Edition
Macro expansion is the recursive process of replacing a macro invocation and any expandable macros in its arguments with source tokens.
- How it is used
- Defines how function-like macro invocations become source tokens during preprocessing.
- How deep it goes
- Explains argument parsing, recursive substitution, rescanning, recursion prevention, and early translation-phase timing.
- Examples
- Uses BYTECOPY expansion, nested STRINGIFY and STRGY definitions, __LINE__, and trace output.
- What you can do after
- Evaluate macro invocations by tracing argument expansion, parameter substitution, rescanning, and stringification order.
Idiomatic Rust
A macro expansion is the compiler-generated source or language construct produced from a macro invocation.
- How it is used
- Explains how declarative macros accept token trees, repeat arguments, and generate calls to other macros.
- How deep it goes
- Demonstrates matching, repetition, nesting, and compiler tracing, then stops at the built-in implementation of format_args!.
- Examples
- Uses special_println!, var_print!, formatted output, and trace_macros! expansion output.
- What you can do after
- Evaluate macro behavior by reading repeated token patterns and tracing nested expansions into generated Rust constructs.
Write Powerful Rust Macros
Macro expansion is the process of replacing a macro invocation with the source code generated or transformed by that macro.
- How it is used
- Explains how Rust turns macro invocations into ordinary code before checking, compilation, and execution.
- How deep it goes
- Traces matcher selection, transcriber insertion, preserved punctuation, and expansion inspection, while stopping short of procedural-macro internals.
- Examples
- Uses my_vec!() expanding to Vec::new(), derive-generated implementations, and cargo expand output for debugging.
- What you can do after
- Diagnose macro-generated code with cargo expand, including invalid output and errors originating in generated functions.
Read how Write Powerful Rust Macros explains macro expansion →
Clojure: The Essential Reference
Macro expansion is the compile-time transformation of a macro invocation into the expression that will be evaluated.
- How it is used
- Supports REPL exploration, macro writing, debugging, and some code-analysis tooling.
- How deep it goes
- Explains one-step, repeated outer, and recursive expansion, stopping short of evaluating or fully analyzing generated code.
- Examples
- Uses when-first and and forms to compare expansion depth and expose short-circuit implementation.
- What you can do after
- Diagnose macro behavior by inspecting returned forms and compare expansion depth with named Clojure functions.
Read how Clojure: The Essential Reference explains macro expansion →
Related concepts
- macroproduced by · A macro is a named construct that expands or transforms source code, commands, or templates before or during execution.
- cargo-expandused by · cargo-expand is a Cargo subcommand that displays the source code generated by expanding Rust macros.
- logical AND operatorimplemented by · A logical AND operator is a Boolean conjunction that produces true exactly when all operands have true truth values.
- macro invocationproduced by · A macro invocation is source syntax that requests expansion of a macro, typically with an associated name and arguments.
- replacement textuses · Replacement text is the sequence of preprocessing tokens specified by a macro definition for insertion when the macro expands.
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.



