Procedural Macros

During these practical exercises, we will be building some procedural macros. We will need to write new ones during the section dedicated to asynchronous programming.

Exercise 5.a: Create a crate called macros that specifies in its Cargo.toml that it is the source of procedural macros:

[lib]
proc_macro = true

Exercise 5.b: Add the dependencies that we will use to manipulate tokens and the abstract syntax tree, as well as to report errors:

  • proc-macro2 for token manipulation
  • quote for code generation with templates
  • syn with full, visit-mut, and parsing features
  • proc-macro-error for better error messages
  • trybuild to test the errors returned by our macros

Throughout this section, you are encouraged to use sub-modules to store your utility functions. Only the definitions of the procedural macros themselves should be at the top level of the crate.