Rust Declarative Macros: Organization, Scoping, and Common Use-Cases

An Oguri Cap toy from Umamusume standing in front of a fruit market set, holding a mango with a happy expression on her face.

In our previous article, we covered what declarative macros are, and how to define and use them.

In this article, we will focus on how to integrate them into your crate. That means we’ll cover macro scoping, organization, and exporting. We’ll also take a look at some common use-cases with examples.

Hopefully, after reading these two articles, you’ll feel ready to start experimenting with declarative macros in your own crates!

This article is tested under the 2024 edition of Rust.

For older code you may see some older patterns, like using a lot of #[macro_use] statements, but this article is going to focus on more modern practices. If something doesn’t compile for you, check your Rust version and edition.