Procedural Macros: Derive, Function
Dive into procedural macros, including custom `#[derive]` macros and function-like macros, for more complex code generation.
Intro to Procedural Macros
Welcome to the world of procedural macros! Unlike declarative macros (macro_rules!), procedural macros are like functions that operate on Rust code itself.
They take a TokenStream as input, process it, and return another TokenStream. This allows for much more complex code generation.
Types of Procedural Macros
Rust offers three main types of procedural macros:
- Function-like macros: These look and act like regular function calls, e.g.,
my_macro!(...). - Derive macros: These allow you to implement traits automatically for structs and enums using
#[derive(MyTrait)]. - Attribute macros: These allow you to define custom attributes that can be applied to items, e.g.,
#[route("/path")].
All lessons in this course
- Declarative Macros (`macro_rules!`)
- Procedural Macros: Derive, Function
- Interacting with Unsafe Rust