macro

Recursive declarative macros

A recursive macro processes smaller pieces of syntax until a terminal rule matches. Keep recursion bounded; a normal loop is preferable for runtime data. Expected stdout (not executed): '3\n' Common mistake: Recursing without reducing the input or providing an empty base case.

Formal Syntax & Structure

rust syntaxrust
macro_name!($($tail:tt)*)

Working Example

Example Snippetrust
macro_rules! count_labels {    () => { 0usize };    ($head:ident $(, $tail:ident)*) => { 1usize + count_labels!($($tail),*) };}fn main() { println!("{}", count_labels!(red, blue, green)); }
Rust Reference