如何在另一个宏中定义一个具有无限量参数的宏?

Rij*_*kii 4 rust

这有效,但test_macro只接受一个参数:

macro_rules! test_define (
    ($name:ident) => (
        macro_rules! $name (
            ( $x:expr ) => (
                // something
            )
        );
    )
);

test_define!(test_macro);
Run Code Online (Sandbox Code Playgroud)

如果我尝试这样做:

macro_rules! test_define2 (
    ($name:ident) => (
        macro_rules! $name (
            ( $($x:expr),* ) => (
                // something
            )
        );
    )
);

test_define2!(test_macro2);
Run Code Online (Sandbox Code Playgroud)

编译失败:

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

ken*_*ytm 5

嵌套宏不允许重复绑定模式是一个已知的错误(问题#35853).

不幸的是,没有解决方法.唯一的解决方案是将API更改为不依赖于嵌套宏中的重复.