macro

Derive macros: adding implementations

A derive macro contributes additional items without replacing the input type. Label generates an inherent method for a plain unit struct; its toy parser intentionally rejects fields, visibility and generics. Example: two-crate workspace; see fixtures/proc-macro-workspace/consumer/src/bin/derive.rs. This caller is not a standalone rustc program. Expected stdout (not executed): 'Invoice\n' Common mistake: Returning the original struct again from a derive macro, causing duplicate definitions.

Formal Syntax & Structure

rust syntaxrust
#[proc_macro_derive(Label)]

Working Example

Example Snippetrust
use teaching_macros::Label;#[derive(Label)]struct Invoice;fn main() { let _invoice = Invoice; println!("{}", Invoice::label()); }
Rust Reference