How do I define a macro which defines another macro when the inner macro takes arguments?

后端 未结 1 1464
青春惊慌失措
青春惊慌失措 2020-12-20 22:23

Minimal code to reproduce:

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


        
相关标签:
1条回答
  • 2020-12-20 22:59

    This is a known issue (#35853). The current recommended workaround is to pass in the dollar sign $ as a separate token. You can then call yourself, passing in the $:

    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);
    }
    
    0 讨论(0)
提交回复
热议问题