Braces, square brackets and parentheses in macro-call?

I have encountered three forms of macro call:

macro! {...} 

macro! (...)

macro! [...]

they passed the compiler and did the same thing.
So are they the three identical forms to call a macro ?
If not, could you explain for me ? and which one should I prefer ?

1 Like

Yes, they are identical. Use whichever you like best. Usually () is for function-like macros, [] is used for vec![…], and {} is for other cases.


thank you :slight_smile:

Ah, i thought one had to use whichever the macro definition uses. Today i learned...

1 Like

They aren't quite the same. I'm not sure the exact details, but macros invoked with {} can be used as statements, where as macros invoked with [] or () are always expressions.

See Rust Playground for the difference, for example (there is a syntax error in second).

The relevant RFC is I'm not sure if the difference is currently documented anywhere else.


Is it possible to know how the macro got invoked? that is, from within the macro.

In other words, can you write a macro that behaves differently depending on the way it is invoked?

No, the macro doesn't know if it was called with [] {} or ().

From memory, there's no difference between [], {}, or (). The compiler will group all tokens inside one the brackets as a single token tree which then gets given to the macro. So by the time the macro gets the token trees, the original type of brackets being used has been erased.

As @tom.prince mentioned, {} are slightly different in that the parser interprets curly braces as a block. So you can have something like error_chain!{} as a top level item, whereas if I used error_chain!() I'd need to put a semicolon at the end to turn it into a valid statement.