The restriction of what can follow a fragment does not apply to repetition

⚓ Rust    📅 2025-07-25    👤 surdeus    👁️ 3      

surdeus

The little macro book says:

Repetitions also adhere to these restrictions, meaning if a repetition can repeat multiple times(* or +), then the contents must be able to follow themselves. If a repetition can repeat zero times (? or *) then what comes after the repetition must be able to follow what comes before.

Consider this example

macro_rules! impl_d{
    ($($e:expr)* )=>{
        
    };
}
fn main(){
  impl_d!(a b);
}

The rule stmt and expr: =>, ,, or ; does not apply to the repetition. If changing the definition of the macro to the following:

macro_rules! impl_d{
    ($e:expr $e2:expr )=>{
        
    };
}

Then an expected error will be reported

error: `$e:expr` is followed by `$e2:expr`, which is not allowed for `expr` fragments

Is the content of the little macro book outdated?

1 post - 1 participant

Read full topic

🏷️ Rust_feed