🔧

Rust Macro Guide

Verified

by Community

Teaches Rust macro development from declarative macro_rules! macros to procedural derive, attribute, and function-like macros. Covers pattern matching, token trees, hygiene, and debugging techniques for macro development.

rustmacrosmetaprogrammingderivecodegen

Rust Macro Guide

Write powerful macros in Rust to reduce boilerplate and create expressive APIs.

Usage

Describe the repetitive code you want to eliminate or ask about macro concepts for implementation help.

Examples

  • "Write a macro_rules! macro to generate getters for struct fields"
  • "How do I create a derive macro for my trait?"
  • "Debug this macro that isn't expanding correctly"

Guidelines

  • Start with macro_rules! before reaching for procedural macros
  • Explain macro hygiene and how it affects variable scoping
  • Show cargo-expand for debugging macro expansions
  • Cover common patterns: repetition, TT munchers, push-down accumulation
  • Warn about compile-time cost of complex macros