Skip to Content
Course content

65: Macro Basics: Declarative Macros

Click on the "Edit" button in the top corner of the screen to edit your slide content.

You've probably noticed by now that Rust is an incredibly explicit language. I love that about it—nothing happens behind your back—but that explicitness can occasionally lead to some truly mind-numbing boilerplate. I'm talking about those moments where you find yourself typing the same three lines of code ten times in a row, just to initialize a data structure or wrap a series of calls in a specific pattern.

The Tedium of Manual Initialization

Let's look at a common scenario: populating a HashMap. If you're building a configuration map or a lookup table, you've likely done something like this:

let mut settings = HashMap::new();
settings.insert("timeout", 30);
settings.insert("retries", 3);
settings.insert("port", 8080);
settings.insert("buffer_size", 1024);

It works, but it's noisy. You're repeating the variable name settings over and over. If you decide to rename that variable to config_map, you're updating five different lines. Now, you might think, "I'll just write a helper function." But here's the catch: Rust functions require a fixed number of arguments and specific types. To make a helper function for this, you'd have to pass in a vector of tuples, which just moves the boilerplate from the insert calls to the vector declaration. You haven't actually reduced the noise; you've just shifted it.

Shifting the Burden to the Compiler

This is where declarative macros come in. Instead of trying to solve the problem at runtime with a function, we use macro_rules! to solve it at compile time. We're essentially writing a small program that tells the Rust compiler, "Whenever you see this specific pattern of tokens, expand it into this actual Rust code."

Here is how I would implement a map! macro to kill that boilerplate:

macro_rules! map {
    ( $( $key:expr => $val:expr ),* ) => {
        {
            let mut map = HashMap::new();
            $(
                map.insert($key, $val);
            )*
            map
        }
    };
}

// Now we can do this:
let settings = map! {
    "timeout" => 30,
    "retries" => 3,
    "port" => 8080,
    "buffer_size" => 1024,
};

Let's break down what's actually happening here, because the syntax looks like a regex from hell at first glance. The $( ... ),* part is the heart of declarative macros. It's a repetition pattern. I'm telling Rust: "Look for a sequence of expressions ($key:expr), followed by a => token, followed by another expression ($val:expr), and repeat that whole block zero or more times, separated by commas."

The block inside the curly braces is the expansion. Notice the $( ... )* again? That's the repetition operator in action. For every pair the macro matched in the input, it will generate a line of map.insert(...) code. The outer curly braces are crucial—they create a scope so the temporary mut map doesn't leak into your surrounding code.

The Cost of the Magic

I'll be honest with you: macros are a double-edged sword. When you use a function, the compiler can tell you exactly which argument is the wrong type. With macros, you're dealing with "token streams." If you mess up the syntax inside a macro call, the error messages can sometimes be cryptic, pointing to the macro definition rather than the line where you actually made the typo.

Furthermore, macros are harder to read for anyone who isn't familiar with the codebase. When I review a PR, I'm always a bit more skeptical of new macros than I am of new functions. If you can solve the problem with a trait or a generic function, do it. But when you're fighting the syntax of the language itself—like creating a domain-specific language for your config or reducing repetitive initialization—declarative macros are the right tool for the job.




📋 Practical Task

Exercise: Building a Variable-Length Debug Logger

One of the most useful applications for declarative macros is creating a logger that can take any number of key-value pairs and print them in a formatted way without requiring the user to manually build a string or a vector.

Your Task: Implement a macro called log_vars!. The macro should take a series of pairs in the format variable_name => value and print them to the console.

Requirements:

  • The macro should be able to handle any number of pairs (including zero).
  • The output for each pair should be in the format: [LOG] variable_name: value.
  • The macro must use macro_rules! and the repetition pattern $( ... ),*.

Example Usage:

log_vars!( "user_id" => 42, "status" => "active", "attempts" => 3 );
// Expected Output:
// [LOG] user_id: 42
// [LOG] status: active
// [LOG] attempts: 3
Rating
0 0

There are no comments for now.

to be the first to leave a comment.