Rust Macros
Written
— Updated
- Useful Links
- https://danielkeep.github.io/tlborm/book is a useful source of info
- https://github.com/dtolnay/proc-macro-workshop
- Arguments
- Macro argument types
expr
- Any expressionident
- An identifierpath
- A module pathty
- A typeitem
- A top level item (use
,struct Something{}
, etc.)tt
- Any token treeblock
- A code block in brackets
- Argument patterns can have
*
or+
added to it to indicate repetition.
- Macro argument types
- Nested macros
- Macros normally can't call each other if one of the macros is used from another module.
- But you can create another macro rule in the same macro. Traditionally these rules start with @ to reduce chances of accidentally triggering the rule.
- Dealing with tokens
- The
paste
crate lets you concatenate multiple identifiers into a new identifier, which is difficult to do in normal macros.
- The
- Proc Macros
- These are more powerful and are written in Rust.
- They can be function-like macros as with
macro_rules!
or attribute macros like#[test]
, or derive macros to work with#[derive(...)]
. - Since these are effectively compiler plugins, they have to be in a separate crate from the one that uses the macro.
- Useful Crates
syn
helps with parsing Rust source.