4

I'm trying to create a macro to expand a set of names and define fields for a struct:

macro_rules! expand {
  ($($name:ident),*) => {
     pub struct Test {
       $(
          concat_idents!(var_, $name) : Vec<$name>
        ),*
     }
  }
}
//fails
expand!(a,b,c);

This fails as the compiler does not recognize concat_idents! as a macro. How am I supposed to work around this?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
scooterman
  • 1,336
  • 2
  • 17
  • 36

1 Answers1

5

It is not at present possible to do this in any way. Substitutions ($foo) are fine there, but not macro calls.

Chris Morgan
  • 86,207
  • 24
  • 208
  • 215
  • This response is outdated, its perfectly legal to do macro calls inside macro differentiation. I'm using version1.24.0-nightly. – Luke Dupin Dec 25 '17 at 18:56
  • @LukeDupin: no; the answer is still correct, nothing has changed: the parser does not accept macros in field name position. To remove `macro_rules!` from the equation, try `struct Test { concat_idents!(var_, a): u8 }`: It fails to parse. – Chris Morgan Dec 29 '17 at 01:51