Macro
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.
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 and reduce duplication when ordinary functions are insufficient.
- How deep it goes
- Explains macro_rules! patterns, captures, repetition, expansion errors, and standard-library examples, while noting that compiler-built-in implementations may be inaccessible.
- Examples
- Shows a macro returning 6, repeated token inputs, standard-library macro definitions, and generated TryFrom implementations for length-limited string holders.
- What you can do after
- Build basic macros from scratch, read existing macro rules, and use compiler feedback to correct input or generated-code errors.
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
- Uses macros for named constants, platform checks, and interfaces that abbreviate more complex operations.
- How deep it goes
- Explains directive syntax and function-like expansion, then analyzes repeated evaluation and control-flow hazards.
- Examples
- Uses M_PI, BYTECOPY, a repeated-evaluation square macro, and a dangling-else error check.
- What you can do after
- Build simple macros from scratch while inspecting expansion for repeated evaluation and 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 →
100 C++ Mistakes and How to Avoid Them
- How it is used
- Warns against relying on macros because textual substitution can impair correctness, readability, and debugging.
- How deep it goes
- Explains textual substitution and debugging consequences, then contrasts alternatives without detailing preprocessor internals.
- Examples
- Uses PI, SQUARE, FOR, and Boolean macros, then contrasts them with typed C++ replacements.
- What you can do after
- Apply the lesson by replacing simple macros with typed constants, template functions, ordinary loops, and bool values.
Read how 100 C++ Mistakes and How to Avoid Them 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.
Also covered in
- Classic Game Programming on the NES: Make your own retro video game
- Julia as a Second Language
- Code Like a Pro in Rust
What to know first
- functionA function is a callable unit of program code that performs a computation or operation, can accept inputs, and may produce a result or side effect.
- RustRust is a statically typed systems programming language designed for performance, concurrency, and memory safety without requiring garbage collection.
- stringA string is a programming-language value representing textual data as an ordered sequence of characters or encoded units.
- ClojureClojure is a dynamically typed, functional Lisp-family programming language that runs primarily on the Java Virtual Machine and also supports other platforms.
Related concepts
- defnincludes · defn is a Clojure macro that defines a named function and binds it to a Var in the enclosing namespace.
- def macroincludes · The def macro is an Elixir compile-time macro that defines a named function from its name, arguments, and body.
- definlineincludes · definline is a Clojure macro that defines a function eligible for compiler inlining, including for Java interoperability.
- 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.
Questions about macro
What is macro?
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.
Which Manning books explain macro?
Write Powerful Rust Macros, Learn Rust in a Month of Lunches, Idiomatic Rust, Modern C, Cyber Threat Hunting, Elixir in Action, Third Edition, Modern C, Third Edition, Rust in Action: Systems programming concepts and techniques and 2 more.
What should I know before learning macro?
Manning books that teach macro usually introduce function, Rust, string and Clojure first.
What kinds of macro are there?
Kinds of macro include defn, __LINE__, def macro, definline and defmodule macro.
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.









