Search code examples
rustrust-macros

Is macro_rules a regular macro?


I'm trying to understand rust macro syntax. Here I read that macro may be invoked generally in 3 flavours:

mymacro!(<tokens>);
mymacro![<tokens>];
mymacro!{<tokens>};

...and then I see an example macro definition also using macro (macro_rules), but the syntax doesn't conform to these rules:

macro_rules! name {<tokens>}

Is name a token and we have 4-th legal macro invocation form here or macro_rules is rather a keyword than just macro and uses special syntax not available for regular macros?


Solution

  • No, macro_rules is a special "directive". You can check the compiler syntax tree here. The important part is:

    Syntax
    MacroRulesDefinition :
       macro_rules ! IDENTIFIER MacroRulesDef
    

    You can see that it is the entry point of the macros syntax.