New Rustacean e010: Macros rule!

When you use a macro, you actually transform one syntax tree into a different syntax tree.

This is true, but only technically. The input to a macro is a sequence of token trees, not syntax trees. See the Source Analysis: Token Trees section of TLBoRM for diagrams. The important part here is that the input to a macro doesn't have to make any damned sense whatsoever to the compiler, beyond having correct nesting and not containing any invalid tokens. Just in case you don't believe me: Ook!.

They're stable.

Hahahahahahaaaaa (sobs)

...they operate on the abstract syntax tree that the compiler has generated.

Again, not really. They operate primarily on tokens, but the macro can ask the parser to extract an AST node from the token stream; the AST node then turns into a special kind of token. Captures and Expansion Redux goes into this a bit (with more diagrams).

Other macros can be written as standalone items (~11:08)

This vaguely implies that you can write ident macros (name! some_ident { body }) macros with macro_rules!: you can't. You can only create those with compiler plugins.

(talking about try_main!)

You should make the paths absolute; ::std::result::etc rather than std::result::etc. Those paths will only work in the root module, or where std has been explicitly brought into local scope. Here's an example showing how relative std paths don't work everywhere.

... not the first thing you should reach for.

Lies.

1 Like