liveBook · Manning

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.

How 10 Manning books explain it

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.

Read how Write Powerful Rust Macros explains macro →

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.

Read how Learn Rust in a Month of Lunches explains macro →

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.

Read how Idiomatic Rust explains macro →

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.

Read how Modern C explains macro →

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.

Read how Cyber Threat Hunting explains macro →

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.

Read how Elixir in Action, Third Edition explains macro →

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.

Read how Modern C, Third Edition explains macro →

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.

Read how Clojure: The Essential Reference explains macro →

Also covered in

What to know first

Related concepts

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.