macro

Function-like procedural macros

A function-like macro receives tokens from inside its invocation delimiters. Its returned syntax occupies the invocation position. Here seven generates an integer expression. Example: two-crate workspace; see fixtures/proc-macro-workspace/consumer/src/bin/function_like.rs. This caller is not a standalone rustc program. Expected stdout (not executed): '9\n' Common mistake: Expecting the macro function to receive already evaluated Rust values.

Formal Syntax & Structure

rust syntaxrust
#[proc_macro]pub fn name(input: TokenStream) -> TokenStream

Working Example

Example Snippetrust
use teaching_macros::seven;fn main() { let n = seven!() + 2; println!("{n}"); }
Rust Reference