skip to content

Compile-Time Code Generation

Rust macros generate code from token trees, through macro_rules! patterns or compiled procedural macros. Interviewers ask how hygiene works and when a macro beats a function or generic.

on this pageshow

explore

questions

22

In Rust, how is the standard vec! macro defined with macro_rules!, and why does vec![value; n] need Clone?

level: juniorimportance: must knowfreq 60%

answer

  1. arms tried top to bottom
  2. empty, repeat, comma list
  3. optional trailing comma matcher
  4. repeat arm calls from_elem

basics

~20 s

vec! is a macro_rules! macro with three arms tried in order: empty, element-semicolon-count, and a comma-separated list of expressions. The repeat arm calls vec::from_elem, which fills the vector by cloning the element, so vec![x; n] needs x: Clone.

solid answer

~40 s

In `alloc`, `vec!` is an exported `macro_rules!` macro with three arms that rustc tries top to bottom, expanding the first that matches. `()` expands to `Vec::new()`. `($elem:expr; $n:expr)` expands to a call to `vec::from_elem($elem, $n)`, whose signature is `from_elem<T: Clone>(elem: T, n: usize)`. `($($x:expr),+ $(,)?)` matches one or more comma-separated expressions with an optional trailing comma and builds the vector from a boxed array of them. Because the repeat form clones one value, `vec![Mutex::new(0); 3]` fails with E0277 (`Mutex` is not `Clone`), and `vec![Rc::new(0); 3]` holds three handles to one allocation. The element expression is evaluated once, even when `n` is 0, and `n` may be a runtime value. It is a macro because a function cannot take a variable number of arguments.

code

rust · 15 lines
rust
use std::rc::Rc;

fn main() {
    let empty: Vec<i32> = vec![];
    let listed = vec![1, 2, 3,]; // list arm; $(,)? takes the trailing comma
    let names = vec![String::from("x"); 3]; // repeat arm, String: Clone
    println!("{empty:?} {listed:?} {names:?}");

    let shared = vec![Rc::new(0); 3];
    println!("strong={}", Rc::strong_count(&shared[0]));

    let mut calls = 0;
    let none: Vec<i32> = vec![{ calls += 1; 7 }; 0];
    println!("none={none:?} calls={calls}");
}

go deeper

for a junior

Know that vec! is a macro_rules! macro, the two call forms, and that the repeat form clones one value. Be ready to say why a function could not take any number of elements.

for a middle

Walk through the three arms and how rustc tries them in order, explain $( ),+ and $(,)? in the list arm, and connect the repeat arm to from_elem's T: Clone bound and the E0277 it produces.

for a senior

Spot the review bugs the expansion explains: shared Rc handles from the repeat form, side effects at a zero count, and a hand-written variadic macro missing $(,)? that rejects trailing commas.

for a principal

Use vec! as the model for a team's own literal macros: a list arm, an optional repeat arm, a trailing comma, and paths through $crate, so internal macros feel like std rather than surprising callers.

## What `vec!` is `vec!` is not a function: it is a **declarative macro**, written with `macro_rules!` in the `alloc` crate and re-exported by `std`. A declarative macro is a list of **rules** (often called arms). Each rule has a **matcher**, a pattern over the tokens the caller wrote between the brackets, and a **transcriber**, the tokens that replace the call when the matcher succeeds. Matching happens during compilation, before type checking, on tokens rather than on values. ## The three arms in the real definition Abridged from `library/alloc/src/macros.rs` in Rust 1.99.1 (the third arm's body uses internal helpers and is summarised in a comment): ```rust macro_rules! vec { () => ( $crate::vec::Vec::new() ); ($elem:expr; $n:expr) => ( $crate::vec::from_elem($elem, $n) ); ($($x:expr),+ $(,)?) => ( // builds a boxed array [$($x),+] and turns it into a Vec ); } ``` The pieces of the matchers: - **`$elem:expr`** binds a **metavariable** named `elem` to one Rust expression; `expr` is the **fragment specifier**. - **`;`** outside any `$` construct is a literal token that must appear in the call. - **`$( ... ),+`** is a **repetition**: the inner pattern one or more times, separated by commas. - **`$(,)?`** is an optional trailing comma: `?` means zero or one occurrence. - **`$crate`** in the transcribers makes the paths resolve to the defining crate wherever the macro is used (the lesson on macro hygiene and scope covers why). ## How rustc picks an arm The Reference states the rule: the expander tries each rule in turn and transcribes the first successful match. 1. `vec![]` has no tokens, so the empty arm `()` matches. 2. `vec![0; 4]` fails the empty arm, then the second arm parses `0` as `$elem`, sees the literal `;`, parses `4` as `$n` and matches. 3. `vec![1, 2, 3]` fails the empty arm; the second arm parses `1` but finds `,` where it needs `;`, so it fails and the third arm matches, binding `$x` three times. 4. `vec![1, 2, 3,]` also reaches the third arm, whose `$(,)?` absorbs the trailing comma. The Rust Book shows a simplified version with a single arm `( $( $x:expr ),* )` that pushes each element. That version has no `$(,)?`, and rustc 1.99.1 rejects `book_vec![1, 2, 3,]` with "unexpected end of macro invocation": after a separator comma it expects another expression. ## Why the repeat form needs `Clone` The second arm hands one evaluated value and a count to `from_elem`, declared `pub fn from_elem<T: Clone>(elem: T, n: usize) -> Vec<T>`. The vector is filled by cloning that value, and the bound is checked like any other call: | Call | Arm | What it requires | |---|---|---| | `vec![]` | empty | an element type rustc can infer | | `vec![x; n]` | repeat | `x: Clone`, `n: usize`, which may be a runtime value | | `vec![a, b, c]` | list | every element of one type; no `Clone` needed | `vec![Mutex::new(0); 3]` therefore fails with **E0277**: the trait `Clone` is not implemented for `std::sync::Mutex<{integer}>`, with a note pointing at the bound on `from_elem`. The list form `vec![Mutex::new(0), Mutex::new(0)]` compiles, because each element is its own expression. ## Consequences people trip over - **Clones share what `Clone` shares.** The `vec!` docs warn that `vec![Rc::new(1); 5]` makes five references to the same boxed integer; `Rc::strong_count` on any slot reads 5. - **The element is evaluated once.** `vec![make(); 3]` calls `make` once and clones the result, unlike the list form, which evaluates each expression. - **A zero count still evaluates the element.** The docs say `vec![expr; 0]` evaluates `expr` and immediately drops it, so side effects still happen. - **The count need not be constant.** An array repeat expression `[x; N]` needs a constant length and, for a length above 1, a `Copy` operand, a const block or a path to a constant item; `vec!` needs only `Clone` and any `usize`. ## Why a macro and not a function Rust functions have a fixed number of parameters, so no function can accept `vec![1, 2, 3]` and `vec![1, 2, 3, 4, 5]` alike. A `macro_rules!` repetition matches any number of expressions and expands to code specialised for that call, which is also why `vec!` can offer two different syntaxes, a list and a repeat, under one name.

  • Why does vec![1, 2, 3,] compile while the Rust Book's simplified vec! rejects it?
    std's list arm is `($($x:expr),+ $(,)?)`, so an optional comma may follow the last expression. The book's single arm `( $( $x:expr ),* )` treats every comma as a separator, so after the final comma it expects another expression and rustc reports "unexpected end of macro invocation". Adding `$(,)?` after the repetition is the usual fix.
  • When would you write vec![a, b, c] instead of vec![x; 3] even though both give three elements?
    When each element must be independent or the type is not `Clone`. The repeat form evaluates `x` once and clones it, so `Rc` handles share one allocation and a `Mutex` will not compile. The list form evaluates each expression separately and only needs the elements to share one type.

  • vec! is a variadic function in the standard library
  • vec![make(); 3] calls make three times
  • vec![Rc::new(0); 3] creates three separate allocations
  • vec![expr; 0] never evaluates expr
  • The count in vec![x; n] must be a compile-time constant
open as a page

In Rust, how do you write a macro_rules! hashmap! literal macro that accepts key => value pairs with an optional trailing comma?

level: middleimportance: must knowfreq 50%

basics

~10 s

Match $($key:expr => $value:expr),* $(,)? and transcribe a block that creates a HashMap, repeats map.insert($key, $value); once per pair, and returns the map. The inner braces make the expansion one block expression.

open as a page

A Rust utility crate exports `log_once!`, which calls the crate's own helper function; why must the macro write `$crate::` instead of `crate::` or a bare name?

level: middleimportance: must knowfreq 50%

basics

~20 s

Paths in a macro_rules! body resolve where the macro is invoked, so a bare helper name or crate:: points into the caller's crate and fails with E0425. $crate always means the defining crate, though the helper must still be pub.

open as a page

In Rust, why can code after a `macro_rules!` call not read a variable the expansion declared with `let`, and how do you hand one out?

level: middleimportance: must knowfreq 45%

basics

~20 s

macro_rules! hygiene resolves local variables and labels where the macro was defined, so a let inside an expansion is invisible to the caller (E0425) and never captures the caller's variables. To hand one out, take its name as a $name:ident fragment.

open as a page

In Rust, what does a custom derive such as #[derive(Builder)] do at compile time, and can it change the struct it annotates?

level: juniorimportance: should knowfreq 45%

basics

~20 s

A custom derive is a function in a proc-macro crate that rustc runs while compiling: it receives the struct's tokens and returns new items, which rustc appends after the struct. It cannot edit or remove the struct itself.

open as a page

In Rust, why must a `macro_rules!` macro be defined above its first use in a file, when a function can be defined anywhere?

level: juniorimportance: should knowfreq 40%

basics

~20 s

A macro_rules! macro has textual scope: it exists from its definition to the end of the enclosing module or block, so a call above it fails to resolve. A function is an item with path-based scope, visible anywhere in its module.

open as a page

In Rust, how does a function-like procedural macro such as `sql!("SELECT id FROM users")` check a query at compile time, and what limits its input?

level: middleimportance: should knowfreq 40%

basics

~20 s

A #[proc_macro] function gets the tokens inside sql!( ... ), runs Rust over them during compilation, and returns tokens that replace the call, or a compile_error!. The input must lex as Rust tokens, so SQL travels as a string literal.

open as a page

In Rust, what does a `#[proc_macro_attribute]` function receive for `#[route(GET, "/users")] fn list_users()`, and what replaces that function?

level: middleimportance: should knowfreq 48%

basics

~10 s

It receives two TokenStreams: the attribute's arguments (GET, "/users") and the whole decorated item with its later attributes. Whatever it returns replaces the item, so the macro must re-emit the function to keep it.

open as a page

In Rust 2024, why can a macro_rules! macro send a const { … } or _ argument to a different arm, and what does expr_2021 keep?

level: middleimportance: should knowfreq 30%

basics

~20 s

In the 2024 edition the expr fragment also matches const blocks and _ expressions, so an earlier $e:expr arm can now win over a later const or _ arm. expr_2021 keeps the old matching and exists for migration.

open as a page

In Rust, a macro_rules! macro generates newtype IDs such as UserId(u64); which fragment specifiers should capture the name, the inner type and the visibility?

level: middleimportance: should knowfreq 40%

basics

~20 s

Capture the name with ident, the wrapped type with ty and the visibility with vis, plus $(#[$attr:meta])* for attributes. Each specifier must produce what the output position needs: a struct name is an identifier, a field is a type.

open as a page

In a Rust derive macro, how do you accept a #[builder(default)] field attribute, and why does rustc reject it as unknown without that?

level: middleimportance: should knowfreq 30%

basics

~20 s

Declare it in the derive as #[proc_macro_derive(Builder, attributes(builder))]. That registers builder as an inert helper attribute on the item and its fields; without it rustc treats #[builder] as an unknown attribute. The derive then finds and parses it in each Field's attrs.

open as a page

How do you write a Rust #[proc_macro_derive] function with syn and quote that generates a builder for a named-field struct?

level: middleimportance: should knowfreq 40%

basics

~20 s

Parse the input with syn's parse_macro_input!(input as DeriveInput), take the struct's ident and named fields from its data, build the output with quote! using #name interpolation and #( ... )* repetitions, then return it with .into().

open as a page

After a Rust workspace adopts a `#[route]` attribute macro built on syn with its `full` feature, builds slow down — where does the time go, and how do you cut it?

level: seniorimportance: should knowfreq 35%

basics

~20 s

The time goes to building the macro crate and syn's full syntax tree before any user crate starts, re-running the macro on every compile, and compiling the code it emits. Cut it by parsing less, enabling fewer features and generating less code.

open as a page

How do you test a Rust attribute macro such as `#[route]`, including its compile errors, when `proc_macro` APIs panic outside a macro?

level: seniorimportance: should knowfreq 30%

basics

~20 s

Write the transformation over proc_macro2 types and unit-test it with syn::parse2, check behaviour and error messages with trybuild's pass and compile_fail cases against .stderr snapshots, and inspect the generated code with cargo expand or nightly -Zunpretty=expanded.

open as a page

In Rust, why does rustc reject a JSON-style hashmap macro_rules! matcher $($key:expr : $value:expr),*, and how do you fix it?

level: seniorimportance: should knowfreq 30%

basics

~20 s

An expr fragment may only be followed by =>, a comma or a semicolon, so rustc rejects the colon when it reads the macro definition. Use => as the separator, or capture keys as literal or tt, which any token may follow.

open as a page

A Rust derive's error about one field points at #[derive(Defaults)] instead of the field; how do syn::Error spans and quote_spanned! fix that?

level: seniorimportance: should knowfreq 25%

basics

~20 s

Tokens written inside quote! get Span::call_site(), the derive invocation, so rustc blames #[derive(...)]. Report your own checks with syn::Error::new_spanned(node, msg).to_compile_error(), and wrap per-field code in quote_spanned!{field.ty.span()=> ...} so rustc's own errors land on the field.

open as a page

A Rust derive emits `impl #name { ... }` and fails with E0107 on `struct Labeled<T>`; why, and how does syn's split_for_impl fix it?

level: seniorimportance: should knowfreq 30%

basics

~20 s

The generated header names Labeled without its parameters, so rustc sees impl Labeled and reports missing generics. Generics::split_for_impl returns the declared parameters with bounds, the bare argument list, and the where clause, to place as impl #impl_generics #name #ty_generics #where_clause.

open as a page

A Rust `macro_rules!` macro emits `static ONCE` at statement level, and two calls in one function fail with E0428; why, and what fixes it?

level: seniorimportance: should knowfreq 25%

basics

~20 s

macro_rules! hygiene covers only local variables and labels; statics, functions and other items are named as if written at the call site, so two expansions put ONCE into one block twice (E0428). Wrap the expansion in its own block, or take the name as an ident.

open as a page

In a Rust crate, a `macro_rules!` macro in `src/macros.rs` cannot be found from `src/report.rs`; which fixes work, and how do they differ?

level: seniorimportance: should knowfreq 35%

basics

~20 s

Textual scope ends when the macros module closes. #[macro_use] on mod macros extends it to code declared after that line; pub(crate) use name; gives the macro an ordinary path to import; #[macro_export] moves it to the crate root and exports it to other crates.

open as a page

Your Rust team is offered a `#[route]` attribute macro and an `sql!` checker; when do you accept a procedural macro over a generic API or `macro_rules!`?

level: principalimportance: should knowfreq 25%

basics

~20 s

Accept one only when a function, a generic API or macro_rules! cannot do the job: reading an item, generating new identifiers, or validating a language at compile time. Keep it thin over a normal API and budget its build and tooling cost.

open as a page

In Rust, a hashmap! macro sizes its map through a recursive internal @count rule; how does that work, and why does it fail for around 128 entries?

level: seniorimportance: nice to knowfreq 20%

basics

~20 s

The @count arms peel off one token tree and call the macro again on the rest, adding 1 each time. Each call nests one expansion deeper, so a literal with about 128 entries exceeds rustc's default recursion limit of 128.

open as a page

A Rust 2021 crate exports a macro that writes `let gen = $e;` and a plain `extern "C"` block; why does it compile when a 2024 crate invokes it?

level: seniorimportance: nice to knowfreq 15%

basics

~20 s

Edition hygiene: every token remembers the edition of the crate it was written in, and rustc parses it under that edition wherever it is expanded. The macro's own tokens follow 2021 rules, where gen is an identifier and extern blocks need no unsafe.

open as a page