0PricingLogin
Learn Rust Coding · Lesson

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

  1. Declarative Macros (`macro_rules!`)
  2. Procedural Macros: Derive, Function
  3. Interacting with Unsafe Rust
← Back to Learn Rust Coding