Declarative macro metavariable repetition levels

⚓ Rust    📅 2025-12-03    👤 surdeus    👁️ 1      

surdeus

Hi,

since when does this code work?

macro_rules! distribute {
    { $($fun:ident($($arg:ident)*))* } => {
        stringify!($($($fun $arg),*);*)
    }
}

fn main() {
    println!("{}", distribute!{ f(a b c) g(d e) });
}

Originally I thought it wouldn't work because the $fun metavariable is defined in one level of repetition ($(_)*) but expanded in two ($($(_)*)*), but to my surprise it works well.

Even the reference says:

A metavariable must appear in exactly the same number, kind, and nesting order of repetitions in the transcriber as it did in the matcher

So I guess this is a (relatively) recent change and the reference is not yet up to date. Is that correct? When did the change happen?

(Not complaining about this one, but I wonder what other possibilities am I missing)

4 posts - 4 participants

Read full topic

🏷️ Rust_feed