agentleFS
gurinderugurinderu/craft/skills/rust-macros/SKILL.md

rust-macros

Skill1 starsChanged 40 days agoMITView on GitHub

Rust metaprogramming — declarative macros (macro_rules!) and procedural macros (derive, attribute, function-like) with syn/quote, plus when a macro is the wrong tool. Use when writing or debugging a macro, generating boilerplate, building a derive, or deciding macro vs generics. Triggers: macro_rules!, proc macro, derive macro, syn, quote, cargo expand.

  • Installs packages

7 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use rust-observability.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.

The file

We split this skill into the rules it gives an agent

2 rules. 0 are common, 1 is distinctive, and 1 is very specific to this project.

  • 0

    Common

    Most of the 6 edited versions we read keep these.

  • 1

    Distinctive

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Use macro_rules! when the variation is in the *syntax/shape*.

    line 57
  2. Distinctive:

    When a macro misbehaves, expand it first.

    line 69

“Common” is measured: kept by at least 6 in 10 of the 6 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Rust Macros

Macros generate code at compile time. They're powerful and easy to overuse — they hurt readability, tooling, and compile times, and they're harder to debug than ordinary code. Reach for them only when functions, generics, and traits genuinely can't express the thing. Procedural macros are in proc-macros.md.

When to Use

  • Eliminating boilerplate that can't be a function/generic (varies in arity or by type shape)
  • A custom #[derive(...)] for your trait
  • A small embedded DSL (vec!-like, builder syntax)
  • Annotating items to transform/register them (attribute macros)

First: do you actually need a macro?

Escalate up this ladder and stop at the first rung that works — each step costs more:

1. a function                  — runtime values
2. generics + traits           — same code over many types (→ rust-traits)
3. macro_rules! (declarative)  — same SYNTAX over many forms; variadic; light DSL
4. a procedural macro          — parse/transform arbitrary tokens; custom derive

Most "I need a macro" is really generics (rung 2). A macro that just calls a function with fixed args is a function. Don't pay the macro tax for something the type system already does.

Declarative macros — macro_rules!

Pattern-match on syntax and expand. Good for variadic helpers and reducing repetitive call sites.

macro_rules! hashmap {
    ($($key:expr => $val:expr),* $(,)?) => {{   // trailing-comma tolerant
        let mut m = std::collections::HashMap::new();
        $( m.insert($key, $val); )*             // repetition expands per element
        m
    }};
}
let m = hashmap!{ "a" => 1, "b" => 2 };
  • Fragment specifiers name what a metavariable matches: expr, ident, ty, pat, tt, literal, block, path, … Choose the tightest one (tt is the catch-all, last resort).
  • Repetition: $(...)* / + / ? with a separator, mirrored in the body.
  • Hygiene: identifiers you introduce don't collide with the caller's — a real safety feature declarative macros give you for free.
  • Export with #[macro_export] (it lands at crate root).

Use macro_rules! when the variation is in the syntax/shape; if you need to inspect types, fields, or generate impls from a struct's definition, you've outgrown it → procedural macros.

See what a macro expands to

The #1 debugging tool — read the generated code:

cargo install cargo-expand
cargo expand path::to::module        # prints the post-macro source

When a macro misbehaves, expand it first; the bug is almost always visible in the output.

Boundaries

  • Procedural macros (derive/attribute/function-like, syn/quote, errors, testing) → proc-macros.md.
  • "Same logic over many types" without code generation → rust-traits (generics/traits first).
  • A macro that emits unsafe carries the same obligations → rust-unsafe.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.