liveBook · Manning

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.

How 4 Manning books explain it

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.

Read how Modern C, Third Edition explains macro expansion →

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.

Read how Idiomatic Rust explains macro expansion →

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

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.