liveBook · Manning

Procedural macro

A procedural macro is Rust code that transforms an input token stream into generated code during compilation.

How 2 Manning books explain it

Write Powerful Rust Macros

A procedural macro is a Rust macro implemented as code that receives a token stream and programmatically produces another token stream.

How it is used
Generates compiler-checked Rust code when declarative macros cannot express required transformations.
How deep it goes
Explains TokenStream input and output, lexing, parsing, setup, testing, and expansion inspection without claiming full compiler internals.
Examples
Builds a Hello derive macro that adds behavior to struct Example through a macro library and consumer application.
What you can do after
Build it from scratch as a derive-macro library, test generated code, and inspect expansions with cargo expand.

Read how Write Powerful Rust Macros explains procedural macro →

Idiomatic Rust

A procedural macro is a Rust macro implemented as code that programmatically transforms a token stream into generated Rust code.

How it is used
Generates Rust code at compile time for language extensions and repetitive implementations, particularly derives.
How deep it goes
Works through raw token output and a derive implementation with parsed input and generics, but limits treatment to basics.
Examples
Demonstrates a greeting macro and a PrintName derive alongside its generated trait implementation.
What you can do after
Build a basic function-like macro or derive macro in a procedural-macro library crate.

Read how Idiomatic Rust explains procedural macro →

What to know first

Related concepts

Questions about procedural macro

What is procedural macro?

A procedural macro is Rust code that transforms an input token stream into generated code during compilation.

Which Manning books explain procedural macro?

Write Powerful Rust Macros and Idiomatic Rust.

What should I know before learning procedural macro?

Manning books that teach procedural macro usually introduce Rust and macro first.

What kinds of procedural macro are there?

Kinds of procedural macro include attribute macro, derive macro and function-like macro.

What is procedural macro a kind of?

Procedural macro is a kind of 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.

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.