Macro
A macro is a named construct that expands or transforms source code, commands, or templates before or during execution.
Write Powerful Rust Macros
A Rust macro is a compile-time language construct that generates or transforms Rust source code through expansion.
- How it is used
- Uses macros to remove boilerplate, generate APIs, customize code through attributes, and create DSLs when ordinary Rust becomes insufficient.
- How deep it goes
- Explains compile-time expansion, declarative matching, procedural macro categories, testing, debugging, customization, and production concerns.
- Examples
- Uses JSON request handling, a custom vector macro, builder derives, field attributes, infrastructure DSLs, and YAML configuration.
- What you can do after
- Build it from scratch: create, test, debug, customize, document, and prepare Rust macro libraries for publishing.
Learn Rust in a Month of Lunches
A Rust macro generates or transforms source code during compilation.
- How it is used
- Uses macros to generate code that functions cannot and to reduce repetitive implementations.
- How deep it goes
- Works through macro_rules! patterns, fragment types, repetition, compiler errors, and library definitions, stopping where compiler-built-in macros hide details.
- Examples
- Shows println!, a macro returning six, generated functions, standard-library rules, and repeated string-holder conversions.
- What you can do after
- Build it from scratch: write basic macro_rules! rules, read accepted inputs, and generate repeated Rust definitions.
Idiomatic Rust
- How it is used
- Generates repetitive Rust code and supports variadic calls or small domain-specific languages where functions are less suitable.
- How deep it goes
- Works through declarative matcher fragments, repetition, expansion and builder methods; defers procedural macros.
- Examples
- Uses `println!` wrappers, identifier-printing macros, `lazy_static!` declarations and generated builder methods.
- What you can do after
- Build it from scratch with `macro_rules!` for variadic calls or repeated methods, while judging when an existing crate is preferable.
Modern C
A define directive is a C preprocessor directive that defines a macro and its replacement text.
- How it is used
- Supplies textual replacements for constants, function-like interfaces, and preprocessor checks while demanding care around hidden behavior.
- How deep it goes
- Works through directive syntax, argument-expansion steps, repeated evaluation, and dangling-else behavior.
- Examples
- Uses a double-constant definition, minimum-size and byte-copy replacements, a counter-based squaring comparison, and a dangling-else expansion.
- What you can do after
- Build it from scratch with `#define`, trace argument replacement, and avoid repeated evaluation or surprising control flow.
Cyber Threat Hunting
A macro is an embedded program or sequence of commands that automates actions within an application or document.
- How it is used
- Uses macro-enable evidence to investigate a suspicious Word document’s execution timeline.
- How deep it goes
- Interprets process and registry telemetry around macro use but does not inspect macro code or document contents.
- Examples
- Follows critical_list.doc from Word opening and macro enablement to cscript, PowerShell, and rundll32 execution.
- What you can do after
- Diagnose/debug with Sysmon evidence by correlating macro-related Word activity with subsequent process execution.
Elixir in Action, Third Edition
A macro system is a language facility that transforms code during compilation or another pre-execution phase.
- How it is used
- Explains compile-time transformation as a way to reduce boilerplate, define expressive constructs, and support production abstractions.
- How deep it goes
- Defines AST-based transformation and demonstrates expansions, but deliberately stops short of detailed macro implementation.
- Examples
- Uses unless-to-if expansion, use GenServer callback injection, and ExActor-based server simplification as concrete examples.
- What you can do after
- Apply the use macro with GenServer to obtain default callbacks and override only the behaviour required by a module.
Modern C, Third Edition
A macro definition is a preprocessing directive that associates a macro name and optional parameters with replacement text.
- How it is used
- Provides textual constants, function-like interfaces, type-generic functions, and selected compiler or library mechanisms.
- How deep it goes
- Defines replacement structure and function-like expansion rules, then stops short of a complete preprocessing specification.
- Examples
- Uses M_PI, BYTECOPY, argument substitution, hidden return control flow, and dangling-else examples.
- What you can do after
- Build it from scratch by defining textual constants and function-like replacements with explicit argument and control-flow safeguards.
Rust in Action: Systems programming concepts and techniques
A macro is a code-generation construct that expands source syntax into program code rather than directly returning a runtime value.
- How it is used
- Simplifies common Rust patterns and console output while introducing the ! syntax.
- How deep it goes
- Defines macros by contrasting them with functions and sketches type detection, but defers detailed expansion mechanics.
- Examples
- Uses println!, eprintln!, formatted placeholders, and a multilingual greeting program as concrete examples.
- What you can do after
- Apply it via println! and eprintln! to generate formatted console-output code without writing type-specific boilerplate.
Read how Rust in Action: Systems programming concepts and techniques explains macro →
Clojure: The Essential Reference
A macro is a program construct that transforms unevaluated source forms before they are evaluated.
- How it is used
- Transforms source syntax at compile time to reduce repetition, alter evaluation flow, or express DSLs.
- How deep it goes
- Explains definition, expansion, quoting, hygiene, and debugging tools, while deferring detailed implementation examples.
- Examples
- Uses threading macros, defmacro, macroexpand, quote, apply-template, do-template, gensym, and destructuring as representative facilities.
- What you can do after
- Build it from scratch with defmacro, inspect expansions with macroexpand, preserve forms with quote, and generate unique names with gensym.
Related concepts
- defnincludes · defn is a Clojure macro that defines a named function and binds it to a Var in the enclosing namespace.
- defmacroproduced by · defmacro is a Clojure special form or macro-definition facility that defines a macro and binds it to a var in the current namespace.
- setjmp/longjmpincludes · The setjmp/longjmp mechanism is a C nonlocal-control-flow facility that saves an execution context and later restores it to transfer control across function-call boundaries.
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.








