Declarative Macros in Rust: Are They Just Elegant Match Arms?
By Ugochukwu Chizaram Omumusinachi. Updated Mon Jul 07 2025I read The Little Book of Rust Macros so you don't have to. Today, we're diving into one of Rust's most powerful and sometimes intimidating features: declarative macros.
TL;DR: They are just elegant match
statements for your code. Do you doubt it? Say, riddle me.
If you've spent any time with Rust, you've used macros. Every time you write println!("Hello, {}!", name)
or vec![1, 2, 3]
You're using one. In simple terms, a macro is code that writes other code at compile time. The compiler sees your macro call, expands it into actual Rust code based on a set of rules, and then compiles that resulting code. This process, called meta-programming, allows you to write more expressive and less repetitive code.

So, why not just write a function? Because macros can do things functions can't.