macro
Attribute macros that extend an item
An attribute macro can return the original item together with generated sibling items. The add_marker example adds one fixed constant and must only be used once in a module, because the fixed name can collide. Example: two-crate workspace; see fixtures/proc-macro-workspace/consumer/src/bin/attribute_addition.rs. This caller is not a standalone rustc program. Expected stdout (not executed): '9\n' Common mistake: Applying the attribute multiple times in one module without accounting for duplicate generated names.
Formal Syntax & Structure
rust syntaxrust
item.extend(extra_tokens);Working Example
Example Snippetrust
use teaching_macros::add_marker;#[add_marker]fn amount() -> u32 { 4 }fn main() { println!("{}", amount() + GENERATED_MARKER); }