Skip to content
Skills Library
Programming · Building & coding

rs-macros-const

/rs-macros-const

Use when writing or reviewing macros, derive/proc macros, build scripts or compile-time computation — macro_rules!, declarative vs procedural macros, derive macros, attribute macros, syn/quote, hygiene, trybuild tests, cargo expand, const fn, const generics, const evaluation, compile-time assertions, type-level programming, build.rs code generation. Triggers: macro, macro_rules, declarative macro, procedural macro, proc-macro, derive macro, attribute macro, syn, quote, proc_macro2, darling, hygiene, cargo expand, trybuild, build.rs, codegen, const fn, const generics, const eval, const assert,

Repository
rs-skills ↗
Licence
MIT
Last repository change
11.11.2026

Assessment and security

100/ 100Class: good
Format25 / 25
Frontmatter with name and description, valid name, description up to 1,024 characters, name matching the folder.
Discoverability25 / 25
Description explains when to use the skill, has a reasonable length and uses third person.
Content35 / 35
At least 150 words and at most 500 lines, headings, step lists, examples or code.
Hygiene15 / 15
No placeholders such as TODO, no Windows paths, with a licence.
Riskno signals detected in the scan
Files in this package
8
Scripts
0
HTML comments
2
URLs in SKILL.md
0
Deprecation

No signals detected. Repository changed on 11.11.2026, without references to retired models or APIs.

Licence

MIT, open: use and modify while retaining author attribution.

What to check before installing
  • SKILL.md zawiera komentarze HTML (2). Nie widać ich w podglądzie Markdown, a model je czyta.

What it does and when to use it

Use when writing or reviewing macros, derive/proc macros, build scripts or compile-time computation — macro_rules!, declarative vs procedural macros, derive macros, attribute macros, syn/quote, hygiene, trybuild tests, cargo expand, const fn, const generics, const evaluation, compile-time assertions, type-level programming, build.rs code generation. Triggers: macro, macro_rules, declarative macro, procedural macro, proc-macro, derive macro, attribute macro, syn, quote, proc_macro2, darling, hygiene, cargo expand, trybuild, build.rs, codegen, const fn, const generics, const eval, const assert,

Original author description

Use when writing or reviewing macros, derive/proc macros, build scripts or compile-time computation — macro_rules!, declarative vs procedural macros, derive macros, attribute macros, syn/quote, hygiene, trybuild tests, cargo expand, const fn, const generics, const evaluation, compile-time assertions, type-level programming, build.rs code generation. Triggers: macro, macro_rules, declarative macro, procedural macro, proc-macro, derive macro, attribute macro, syn, quote, proc_macro2, darling, hygiene, cargo expand, trybuild, build.rs, codegen, const fn, const generics, const eval, const assert,

How it works

  1. Core Question
  2. The ladder (stop at the first that fits)
  3. Thinking Prompt
  4. Rules of thumb
  5. Debugging
  6. Trace
  7. Rules (4)
  8. Related Skills
Original SKILL.md headings · 87 lines, 706 words
SKILL.md preview
---
name: rs-macros-const
description: "Use when writing or reviewing macros, derive/proc macros, build scripts or compile-time computation — macro_rules!, declarative vs procedural macros, derive macros, attribute macros, syn/quote, hygiene, trybuild tests, cargo expand, const fn, const generics, const evaluation, compile-time assertions, type-level programming, build.rs code generation. Triggers: macro, macro_rules, declarative macro, procedural macro, proc-macro, derive macro, attribute macro, syn, quote, proc_macro2, darling, hygiene, cargo expand, trybuild, build.rs, codegen, const fn, const generics, const eval, const assert, static_assertions, typenum, generic-array, compile-time, type-level, repetition $(...)*, token tree, macro expansion error"
user-invocable: false
---

# Macros and Compile-time Computation

> **Layer 1/2: Mechanics + Design** · code that writes code, and code that runs before the program does
>
> Depth: `guides/macro-rules.md` · `guides/proc-macros.md` · `guides/const-eval.md`

## Core Question

**Can plain code (a function, a generic, a trait, a `const`) do this? If not, which is the smallest tool that can?**

Macros are the last resort in the abstraction ladder: they hide control flow, produce poor error messages, defeat IDE navigation and slow compiles. Use the lowest rung that works.

## The ladder (stop at the first that fits)

| Rung | Use when | Notes |
|---|---|---|
| 1. function | behaviour only | |
| 2. generics + traits | behaviour over types | zero-cost, great errors (rs-traits) |
| 3. `const fn` / `const` items / const generics | values or sizes known at compile time | `guides/const-eval.md` |
| 4. `macro_rules!` | variadic arguments, repeating a pattern over items, tiny DSL, reducing boilerplate impls for many types | `guides/macro-rules.md` |
| 5. `#[derive]` proc-macro | trait impl derivable from a struct's shape | `guides/proc-macros.md` |
| 6. attribute / function-like proc macro | rewriting items, DSLs that must parse real syntax | high cost; own crate |
| 7. `build.rs` codegen | generating Rust from external schemas (protobuf, OpenAPI, GCL), embedding assets | keep it minimal (rs-structure `proj-build-rs-minimal`); generated code is exempt from Design Contracts — mark it `@generated` |

Before writing rung 5–7, **search for an existing derive/crate** (rs-reuse-first): `serde`, `thiserror`, `clap`, `strum`, `derive_more`, `bon`/`typed-builder`, `prost-build`/`tonic-build`, `utoipa`.

## Thinking Prompt

1. What does the call site look like, and what error does a user get when they misuse it?
2. Is the expansion hygienic — does it use absolute paths (`::std::…`, `$crate::…`) so it works in any scope?
3. How is it tested — expansion output, passing cases, **failing cases** (`trybuild`)?
4. Will a reader understand it without expanding it? (`cargo expand` is for debugging, not for reading.)
5. Does it belong in its own crate (proc-macros must), with a Design Contract stating the grammar it accepts?

## Rules of thumb

| Do | Don't |
|---|---|
| `$crate::path` and `::core::…` paths in macro bodies | rely on the caller's imports |
| keep a macro small and delegate to a generic function (`fn imp(...)`) — less code bloat, better errors | put logic in the macro body |
| emit `compile_error!` / `syn::Error::to_compile_error()` with a span on the offending token | `panic!` in a proc macro (opaque error) |
| document the grammar (examples) in the macro's docs and Design Contract | invent syntax that resembles but differs from Rust |
| test failing inputs with `trybuild` (`.stderr` snapshots) | only test the happy path |
| use `syn`/`quote`/`proc-macro2` (verify the current major — `syn` is at 3.x at the time of writing) and `darling` for attribute parsing | parse tokens by hand |
| use `const` assertions (`const { assert!(…) }`) to fail builds for invalid configuration | find out at runtime |

## Debugging

`cargo expand` (needs nightly or the `cargo-expand` tool), `RUSTFLAGS="-Zmacro-backtrace"` on nightly, `trace_macros!`, minimal reproductions in a scratch crate (rs-cleanup: delete it).

## Trace

| Direction | Go to |
|---|---|
| ↑ Up | rs-api-design (a derive is public API), rs-structure (proc-macro crates live in the workspace as `<name>-macros`) |
| ↓ Down | rs-traits (what the macro should have been), rs-type-driven (const generics, type state), rs-unsafe (macro-generated unsafe) |
| → Delivery | rs-testing (`trybuild`), rs-review (generated code exempt, hand-written macros reviewed) |

<!-- rules-index:start -->
## Rules (4)

Depth rules under `rules/` — open the one the question needs, not all of them.

**const-**

- [`const-assert-invalid-config`](rules/const-assert-invalid-config.md) — Turn invalid compile-time configuration into build errors

**macro-**

- [`macro-hygienic-paths`](rules/macro-hygienic-paths.md) — Use `$crate::` and `::std::` paths inside macro bodies
- [`macro-prefer-generics`](rules/macro-prefer-generics.md) — Reach for a function, generic or `const` before a macro
- [`macro-trybuild-tests`](rules/macro-trybuild-tests.md) — Test what a macro rejects, not only what it accepts

<!-- rules-index:end -->

---

## Related Skills

| When | See |
|---|---|
| Traits and generics first | rs-traits |
| Type-level design (type state, newtypes) | rs-type-driven |
| `build.rs` conventions | rs-structure |
| Existing derives and crates | rs-reuse-first |
Files in this package (8)
  • SKILL.md 5 KB
  • guides/const-eval.md 3 KB
  • guides/macro-rules.md 2 KB
  • guides/proc-macros.md 2 KB
  • rules/const-assert-invalid-config.md 1 KB
  • rules/macro-hygienic-paths.md 1 KB
  • rules/macro-prefer-generics.md 1 KB
  • rules/macro-trybuild-tests.md 1 KB

Versions

The database has no other skills named rs-macros-const.

Database snapshot: 13.11.2026 · package version ec440c1d4fcc (SHA-256 digest of the entire package)

Downloads in the last 30 days: 0 · ranking

: harmful content, incorrect descriptions or an author’s removal request.

Similar skills

  • 100rust-macros
    Design, name, implement, debug, test, and review Rust declarative and procedural macros, including macro_rules matchers and repetition, hygiene, $crate paths, derive, attribute and function-like macros, proc-macro crate naming, syn parsing, quote generation, diagnostics,…
  • 100rust-macros-metaprogramming
    Use this skill when authoring declarative macro_rules! or procedural derive/attribute macros for compile-time generation in Rust.
  • 100macros-code-review
    Reviews Rust macro code for hygiene issues, fragment misuse, compile-time impact, and procedural macro patterns. Use when reviewing macro_rules! definitions, procedural macros, derive macros, or attribute macros.
  • 95rust-macros
    Write a macro only when a function, a trait, or a generic cannot do the job — then by-example before proc-macro, with hygiene, a `_private` helper module, and spanned compile errors instead of panics. Use when writing or reviewing macro_rules! or a proc macro, when a derive or…
  • 93crystal-macros
    Use when implementing compile-time metaprogramming in Crystal using macros for code generation, DSLs, compile-time computation, and abstract syntax tree manipulation.
  • 100better-duck-macros
    Proc-macro surface of the better-duck-macros crate — DuckDB user-defined functions and data derives — consumed through better-duck-core's `feature = "udf"` and `feature = "derive"` (and better-duck-diesel's `derive`), never by depending on better-duck-macros directly. Use when…

Feedback and reports

Your message goes to Bartosz Sokoliński. An email address is needed only for a reply. See the privacy policy.