当内部宏接受参数时,如何定义一个定义另一个宏的宏?

tga*_*tga 5 macros rust

要重现的最小代码:

macro_rules! test {
    ($name:ident: $count:expr) => {
        macro_rules! $name {
            ($($v:expr),*) => {}
        }
    }
}

test!(yo: 123);
Run Code Online (Sandbox Code Playgroud)

得到错误:

error: attempted to repeat an expression containing no syntax variables matched as repeating at this depth
 --> src/lib.rs:4:15
  |
4 |             ($($v:expr),*) => {}
  |               ^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)

删除$count:expr或更改$count:expr为其他类型,如$count:block省略错误,但我真的需要它expr.错误是什么意思?

She*_*ter 5

这是一个已知问题 (#35853)$当前建议的解决方法是将美元符号作为单独的标记传递。然后您可以调用自己,并传入$

macro_rules! test {
    ($name:ident: $count:expr) => { test!($name: $count, $) };

    ($name:ident: $count:expr, $dol:tt) => {
        macro_rules! $name {
            ($dol($v:expr),*) => {}
        }
    };
}

fn main() {
    test!(yo: 2);
    yo!(42);
}
Run Code Online (Sandbox Code Playgroud)