macro

Type and path fragments

Capture a type separately from a path to a named item. The expansion still must type-check after substitution. Expected stdout (not executed): '0 12\n' Common mistake: Capturing a type does not guarantee it implements Default.

Formal Syntax & Structure

rust syntaxrust
$t:ty / $p:path

Working Example

Example Snippetrust
macro_rules! make_default { ($t:ty) => { <$t>::default() }; }macro_rules! apply { ($f:path, $n:expr) => { $f($n) }; }fn triple(n: i32) -> i32 { n * 3 }fn main() { let n = make_default!(u32); println!("{n} {}", apply!(triple, 4)); }
Rust Reference