Macros
A macro generates Rust code at compile time. You call one with !, as in println! or vec!. Macros exist because functions cannot take a variable number of typed arguments or generate impls.
Detailed Explanation
println!("port {}", port) looks like a function call. It is a macro invocation. The ! is the tell. At compile time the compiler expands the invocation into ordinary Rust — a format_args! call, a write to stdout — and then type-checks the result. You cannot write a function that accepts a format string and a matching, typed argument list. A macro can, because it sees tokens, not values.
Rust has two families. Declarative macros, macro_rules!, match the invocation's tokens against patterns and substitute a template. They are the ones you write for vec!-shaped helpers. Procedural macros are Rust functions that take a token stream and return a token stream. They are how #[derive(Debug)], #[tokio::main], and thiserror work. Both run at compile time. Neither is a C preprocessor: they operate on tokens after parsing, not on raw text, so they cannot create an unbalanced brace and they cannot see unparsed garbage the same way #define can.
Macros are hygienic in the declarative case. A variable you bind inside the macro does not collide with a variable at the call site of the same name. You must use $crate to refer to your crate from a macro that other crates call, so the expansion does not look up my_mod in the caller's namespace.
The cost is compile time, error quality, and surprise. A macro can generate a thousand lines. Errors often point at the invocation, not the generated line you would edit. Readers cannot jump-to-definition through a macro as reliably as through a function. That is why the style is: write a function if a function can do it. Write a declarative macro when you need variadic syntax or to implement a trait for a list of types. Write a procedural macro when you need to parse syntax the macro_rules matcher cannot, usually a derive.
When you call a macro: println!, vec!, format!, panic!, assert!, todo! — the standard set. When you write one: after you have tried a function, a generic, and a builder, and the call-site syntax is still the problem.
Code Examples
macro_rules! name_and_len {
($s:expr) => {{
let value = $s;
(value, value.len())
}};
}
fn main() {
let (n, len) = name_and_len!("ada");
println!("{n} {len}"); // prints: ada 3
println!("{:?}", vec![1, 2, 3]); // prints: [1, 2, 3]
}Common Gotchas
The `!` is required
`println(...)` looks for a function. `println!(...)` is the macro. Forgetting `!` is a compile error, not a runtime format.
Prefer a function when a function works
Macros hide types, skip rustfmt in places, and produce worse errors. Variadic formatting is a good reason. Repeating a two-line helper is not.
Expansion errors point at the call site
`cargo expand` (from the cargo-expand crate) shows the generated code when you cannot see the bug.
Declarative macros cannot iterate arbitrary syntax
A list of types is fine. Parsing a mini-language is a procedural macro.