2

I would like to pass mutability to a macro so that I can do

mymacro![mut foo];
mymacro![bar];

and the macro will see them as different matches. which specifier to use?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Yi Zhang
  • 31
  • 4

1 Answers1

3

There isn't one. You'll need two rules: one which matches a literal mut, and one that doesn't.

macro_rules! do_something {
    (mut $name:ident) => { ... };
    ($name:ident) => { ... };
}

And yes, they do have to be in that order, because macro arms are matched top-to-bottom.

DK.
  • 55,277
  • 5
  • 189
  • 162
  • 1
    Is it still the case that they have to be in that order? I have been testing a macro where the rules are in the opposite order and haven't observed any difference. – QuaternionsRock Jan 28 '22 at 21:37
  • @QuaternionsRock No, it would only matter if the macro could match both arms at the same time, in which case the top one would be chosen. In this case, since `mut` can't be an `ident`, there should be no difference. – Filipe Rodrigues Aug 20 '23 at 03:37