How to write a recursive macro_rule go catch the slice inside the slice

Hi guys, I am trying to write a macro_rules that can expand slice to generate some struct.

The simple example I am writing is:

macro_rules! test {
    (&[$($arg:expr),*]) => {
    ($arg:expr) => {
        let a = $arg; // this no sense line just for this test case

What I expect is:

test!(&[&1, 2, 3, &[4, 5]]); will expand to

&[let a = &1, let a = 2, let a = 3, &[let a = &4, let a = &5].to_vec()].to_vec();

but it keep give me the

&[let a = &1, let a = 2, let a = 3, let a = &[4, 5]].to_vec()

Do I make some mistakes in this recursive macro? How can I make it?


I think the inner call will put the whole &[4, 5] "inside" the expr, and the inner-level pattern can't match it anymore. Metavariables and Expansion Redux - The Little Book of Rust Macros

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.