From 4ff304896d5c7e0e30de01506ac95a5c0047d641 Mon Sep 17 00:00:00 2001 From: Huzaifa-Asif Date: Sun, 4 Oct 2026 14:56:27 +0800 Subject: [PATCH] Document procedural macros in expression position --- src/procedural-macros.md | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/procedural-macros.md b/src/procedural-macros.md index 18826351c0..20d3cc022d 100644 --- a/src/procedural-macros.md +++ b/src/procedural-macros.md @@ -57,7 +57,7 @@ r[macro.proc.proc_macro.intro] The *`proc_macro` [attribute][attributes]* defines a [function-like][macro.invocation] procedural macro. > [!EXAMPLE] -> This macro definition ignores its input and emits a function `answer` into its scope. +> These macro definitions ignore their inputs. The first emits a function `answer`, and the second emits the expression `42`. > > > ```rust,ignore @@ -69,19 +69,24 @@ The *`proc_macro` [attribute][attributes]* defines a [function-like][macro.invoc > pub fn make_answer(_item: TokenStream) -> TokenStream { > "fn answer() -> u32 { 42 }".parse().unwrap() > } +> +> #[proc_macro] +> pub fn make_answer_expr(_item: TokenStream) -> TokenStream { +> "42".parse().unwrap() +> } > ``` > -> We can use it in a binary crate to print "42" to standard output. +> We can invoke the first macro in item position and the second in expression position from a binary crate. > > > ```rust,ignore > extern crate proc_macro_examples; -> use proc_macro_examples::make_answer; +> use proc_macro_examples::{make_answer, make_answer_expr}; > > make_answer!(); > > fn main() { -> println!("{}", answer()); +> assert_eq!(make_answer_expr!(), answer()); > } > ```