Macro_rules!: is it possible to match $var:ident value inside macro definition?

How to ckeck if macro receives argument with some defined value and make it behave accordingly?
For example, something like

macro_rules! gender {
    ($g:ident) => ( /* if $g equ "male" then do smth, else do smth else  */ )
}

The best you can do is,

macro_rules! gender {
    (male) => ();
    ($g:ident) => ( /* if $g equ "male" then do smth, else do smth else  */ );
}

You can't always inspect tokens after they are created with declarative macros. If you have a more elaborate syntax in mind, then you want proc macros.

3 Likes

the trick is not to capture it, but instead use it as a "keyword"

macro_rules! ab {
  (a $foo:expr) => (do something);
  (b $foo:expr) => (something else);
}
1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.