macro

Attribute macros: receive and return an item

An attribute macro receives its arguments and the annotated item separately. The keep example validates empty arguments then returns the item without modification. Example: two-crate workspace; see fixtures/proc-macro-workspace/consumer/src/bin/attribute.rs. This caller is not a standalone rustc program. Expected stdout (not executed): '18\n' Common mistake: Returning an empty stream when the intention was to preserve the annotated function.

Formal Syntax & Structure

rust syntaxrust
#[proc_macro_attribute]pub fn name(args: TokenStream, item: TokenStream) -> TokenStream

Working Example

Example Snippetrust
use teaching_macros::keep;#[keep]fn price() -> u32 { 18 }fn main() { println!("{}", price()); }
Rust Reference