Stage 1.12: Macros
We’ve used macros like println!
throughout this stage, but we haven’t fully explored
what a macro is and how it works. The term macro refers to a family of features in Rust: declarative
macros with macro_rules! and three kinds of procedural macros:
- Custom
#[derive]
macros that specify code added with the derive attribute used on structs and enums - Attribute-like macros that define custom attributes usable on any item
- Function-like macros that look like function calls but operate on the tokens specified as their argument
To distinguish it from functions, macros are a way of writing code that writes other code, which is known as metaprogramming
You can write your own macros with macro_rules!
Further information: