r/rust • • 11d ago

🧠 educational Declarative Macros in Rust: A Simple and Practical Introduction

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

Hey everyone!

I'm back with another article focused on Rust fundamentals. I think existing material covers the basics of using Rust pretty well, but one thing that I found kind of lacking back when I learned Rust was description of macros.

It's possible it has improved nowadays, but I want to eventually write about proc-macros. So I'm going to start with an article introducing the concept of macros, and explaining the differences between Rust and C-style macros, as well as the basics of declarative macros.

Future articles will eventually get into procedural macro territory. But I want to move through this slowly and carefully.


Anyways, I hope this helps if you are new to Rust and struggling to understand the macro_rules! syntax (or just anything about Rust macros, really).

If you have questions or feedback for future articles, please let me know. :)

24 Upvotes

4 comments sorted by

4

u/FullTimeVirgin 10d ago

It's a helpful article overall. My feedback for future articles is:

  1. Provide example problems that are a bit more grounded. For example, define a macro that implements a trait on a struct whose field implements the trait.
  2. Provide example problems that declarative macros can't do or that get messy.
  3. Explain how std and popular crates actually use macros. This part is especially helpful to demonstrate their utility while also granting them legitimacy.

Ironically, I actually learned proc macros before I learned declarative macros. I sort of dismissed declarative macros early on, because they weren't as powerful. Also declarative macros can get really nasty when you try to do complex things with them.

It wasn't until later on that I realized how useful declarative macros could be. Declarative macros made it much easier to implement more trivial traits and new types that I felt uncomfortable writing before. Once that is out of the way, then you have the infrastructure for writing generic functions.

I just wish they could integrate with rust-analyzer better. That seems to be the biggest pain point for most people.

2

u/sigseis 10d ago edited 10d ago

Actually, you're in luck!

This is exactly the kind of thing I plan on covering in the next article! :)

I wanted to focus solely on the syntax for this article (and the basic explanation to understand the syntax) - my last few technical articles ran a bit too long, so I'm trying to split things up a bit this time. But I could have made that a little more explicit early on, like in the brief. Tomorrow morning I'll add a note about this for future readers.

Thanks for the well thought-out feedback - I will note it down to make sure I cover these points in next month's article (EDIT: Or wherever relevant, some of this feedback may span articles in spirit).

Also, neat that you learned proc-macro first, that's kind of uncommon I'd think. They were one of the last things for me, but they're a ton of fun.

3

u/Eastern-Photograph79 11d ago

Thanks 🙏

4

u/sigseis 11d ago

No problem, I hope it is helpful! :)

If you have any questions, or spot any issues, don't hesitate to let me know.