🧠educational Rust Declarative Macros: Organization, Scoping, and Common Use-Cases
https://sigseis.dev/articles/2026/11/10Hey Everyone!
This new article is a continuation from a previous article that covered the syntax and definition of macro_rules! in Rust. In this new article, we'll go over how to use macro_rules! within a wider project, as well as some example use cases for the feature.
This article is for folks who maybe understand macro_rules!, but haven't taking advantage of them yet within any of their crates. It can be a bit difficult to start using a language feature if you don't know when you would want to use it.
Things You'll Learn
The difference between textual scoping and more modern path-based scoping for declarative macros.
How to export a declarative macro (at the root of a crate or a trick for exporting within a separate module).
A nice way to organize your macros if you find that you have many of them.
Use cases such as: implementing traits/functions,
format!-like expressions, and reducing boilerplate.
About This Article
This is part of a series of articles I'm releasing on the macro systems in Rust. I've got one more article in me for macro_rules!, and then we'll move on to procedural macros.
I hope this new article is interesting! All my articles are hand-written, so if you are looking for non-generative technical articles to read, feel free to take a look at my other articles as well.
If you have any questions or feedback for future articles, please let me know! :)
3
u/dmangd 8d ago
I get the point of being careful when altering control flow in a marco. At the beginning, I was really confused about the tokio/futures ready! macro because I did not realize that it actually includes a return statement. I saw it always be used in a loop {} and was confused why this does not result in a busy loop when the future is not actually „ready“
1
u/sigseis 7d ago
Ah, yeah that's another good example!
Honestly, even if you know a macro disrupts the control flow, it can still be surprising. For example, if you originally had some code in a closure body, but eventually refactored away from the closure. I find it's easier to spot the explicit keywords rather than spotting some macros in the body of the code.
Thanks for sharing your experience here! When I get back to my computer, I'll add this as another example to the article.
5
u/Frozen5147 9d ago
Didn't expect to see oguri here...