Function-Like Procedural Macros
Function-like procedural macros look and feel like declarative macros to the
caller, but their implementation is a Rust function decorated with the
#[proc_macro] attribute:
use proc_macro::TokenStream;
#[proc_macro]
pub fn make_answer(input: TokenStream) -> TokenStream {
// Process input tokens and return output tokens...
"42".parse().unwrap()
}
Key properties:
- Declaration: Decorated with
#[proc_macro]. - Function Signature: Must accept a single
TokenStream(the tokens inside the brackets of the callsite) and return aTokenStream. - Flexible Invocation: Like other function-like macros, callers can invoke them with any kind of brackets.
This slide should take about 4 minutes.
- Note that the
inputTokenStream contains only the tokens inside the brackets, not the macro name or the exclamation mark. - Emphasize that function-like procedural macros are useful when you need to
parse complex custom DSLs that cannot be expressed easily with declarative
macro_rules!pattern-matching. - Mention that they may expand to expressions, statements, or items.