Procedural derive macros
Quick answer
A derive macro reads the tokens of a struct, enum or union and emits additional Rust code, commonly trait implementations. Built-in derives are a useful introduction before writing a custom procedural macro crate.
Generated implementations and crate boundaries
Custom derive macros live in a separate library crate with [lib] proc-macro = true in its manifest. An exported function with #[proc_macro_derive(Name)] accepts and returns proc_macro::TokenStream; generated code is appended to the input item. For a minimal manual lab, place the second example in src/lib.rs of a scratch proc-macro package called lesson_derive, add a local path dependency from a separate binary package, import lesson_derive::Marker, and apply #[derive(Marker)] to a struct. This intentionally empty expansion is not a useful trait generator.
#[derive(Debug, PartialEq, Eq)]struct Point { x: i32, y: i32 }fn main() { let a = Point { x: 1, y: 2 }; let b = Point { x: 1, y: 2 }; println!("{} {a:?}", a == b);}Expected stdout (not executed):
true Point { x: 1, y: 2 }The derives supply equality and debug formatting implementations.
use proc_macro::TokenStream;#[proc_macro_derive(Marker)]pub fn marker(_input: TokenStream) -> TokenStream { TokenStream::new() }Manual local Rust/Cargo candidate; not executed. Expected behavior:
A separate consumer with the local dependency can derive Marker; no extra items are emitted.This illustrates the compiler entry point without external parsing crates.
Common mistake
A derive macro is not a runtime function, and deriving a trait requires suitable field implementations.
Try it yourself
Derive Clone on a struct containing String and demonstrate that modifying the clone does not modify the original.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Procedural derive macros — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
Where does an exported custom derive implementation belong?
Procedural derive macros — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust #[derive(Debug, PartialEq, Eq)] struct Point { x: i32, y: i32 } fn main() { let a = Point { x: 1, y: 2 }; let b = Point { x: 1, y: 2 }; println!("{} {a:?}", a == b); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Procedural derive macros — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use proc_macro::TokenStream; #[proc_macro_derive(Marker)] pub fn marker(_input: TokenStream) -> TokenStream { TokenStream::new() } ``` Which outcome or interpretation best matches this example?