Nine Rules for Creating Procedural Macros in Rust - by Carl Kadie - Rust Linz, December 2022

Discover the 9 essential rules for creating procedural macros in Rust, including simplicity, testing, and elegance, and learn how to coordinate your projects with Rust's workspaces feature.

Key takeaways
  • The workshop should be about procedural macros in Rust, and there are 9 rules to create them.
  • The first rule is to always develop a procedural macro, and it can be recursive.
  • Any input macro can be created using the PROS macro, which can handle the varieties of input.
  • The second rule is to create a procedural macro, which can handle the conversion among the different types.
  • The third rule is to use a good syntax tree items such as item fn, signature, and block.
  • The fourth rule is to test the procedural macros thoroughly.
  • The fifth rule is to keep the procedural macros simple and avoid unnecessary complexity.
  • The sixth rule is to document the procedural macros well.
  • The seventh rule is to integrate the procedural macros with the rest of the code.
  • The eighth rule is to follow the rules of elegant Rust API design.
  • The ninth rule is to test the procedural macros from the beginning.
  • The top-level project in the workshop has three projects: the derive project, the attribute macro project, and the SYN project.
  • The Rust projects can be coordinated and worked together easily.
  • The UI directory in the Rust project includes UI tests that use the try build crate.
  • There is an option in Rust called workspaces that can coordinate the different projects.
  • It is recommendable to keep the procedural macros simple and easy to understand.
  • There is a challenge in creating procedural macros, but the any input macro can handle it.
  • The any input macro can take any kind of string, including references to strings.
  • There are some efforts to improve debugging in Rust, such as the work done by the Fastlim project.
  • Procedural macros can help in creating unit tests that can be debugged.
  • The any input macro can also handle the conversion among the different types.
  • Error handling is crucial in creating procedural macros, and the any input macro can handle it.
  • There is a need to define the rules of elegant Rust API design.