Macros in Rust: declarative and procedural
Answer: Macros generate code from code (metaprogramming). Declarative macros are the form Rust programmers use most.
macro_rules
Rust's macro system supports metaprogramming. Declarative macros are also called macros by example, macro_rules! macros, or simply macros.
Claims
- id: C1 claim: Macros are a way of writing code that writes other code. source: S1 evidence: "macros are a way of writing code that writes other code, which is known as"
- id: C2 claim: The most widely used form is the declarative macro. source: S1 evidence: "The most widely used form of macros in Rust is the declarative macro."
- id: C3 claim: Rust provides a powerful macro system that allows metaprogramming. source: S2 evidence: "Rust provides a powerful macro system that allows metaprogramming."
Sources
- [S1] Macros. The Rust Project (The Rust Programming Language book). https://doc.rust-lang.org/book/ch20-05-macros.html . License: MIT OR Apache-2.0. Accessed 2026-11-09.
- [S2] macro_rules! - Rust By Example. Rust by Example (rust-lang/rust-by-example). https://doc.rust-lang.org/rust-by-example/macros.html . License: MIT OR Apache-2.0. Accessed 2026-11-09.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources and notices: The Rust Programming Language book (rust-lang/book): Copyright (c) 2010 The Rust Project Developers; MIT OR Apache-2.0, used under MIT (https://github.com/rust-lang/book/blob/main/LICENSE-MIT). Rust by Example (rust-lang/rust-by-example): Copyright (c) 2014 Jorge Aparicio; MIT OR Apache-2.0, used under MIT (https://github.com/rust-lang/rust-by-example/blob/master/LICENSE-MIT). Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: The Rust Project (The Rust Programming Language book), Macros, MIT OR Apache-2.0; Rust by Example (rust-lang/rust-by-example), macro_rules! - Rust By Example, MIT OR Apache-2.0. Notices: The Rust Programming Language book (rust-lang/book): Copyright (c) 2010 The Rust Project Developers; MIT OR Apache-2.0, used under MIT (https://github.com/rust-lang/book/blob/main/LICENSE-MIT). Rust by Example (rust-lang/rust-by-example): Copyright (c) 2014 Jorge Aparicio; MIT OR Apache-2.0, used under MIT (https://github.com/rust-lang/rust-by-example/blob/master/LICENSE-MIT).
Cite this
iinventedlight. 2026. "Macros in Rust: declarative and procedural." iinventedlight Library, iil:note:8z3h5wxb, version of 2026-11-09. https://iinventedlight.com/library/notes/rust-macros/. License: CC-BY-4.0.
@misc{iil_note_8z3h5wxb,
author = {{iinventedlight}},
title = {{Macros in Rust: declarative and procedural}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-09},
url = {https://iinventedlight.com/library/notes/rust-macros/},
note = {Permanent ID iil:note:8z3h5wxb. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/rust-macros.md.