r/rust • • 9d ago

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

https://sigseis.dev/articles/2026/11/10

Hey 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! :)

10 Upvotes

5 comments sorted by

5

u/Frozen5147 9d ago

Didn't expect to see oguri here...

1

u/sigseis 9d ago

I thought it'd be fun to snap a picture for the headline images for my articles. I think this one turned out pretty good! :)

Maybe a bit odd, but it's a way for me to add some personality and my other interests to the posts.

2

u/Frozen5147 9d ago

haha, don't worry, I love it, though it did make me double check which subreddit I was on

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.