# Rust Macros

> Master Rust macros - declarative and procedural macros

- Skill: `pluginagentmarketplace/rust-macros` (Agent Skill, multi-file: 7 files)
- Install (CLI): `npx skillmds@latest add pluginagentmarketplace/rust-macros`
- Raw SKILL.md: https://api.skillmd.com/api/skills/pluginagentmarketplace/rust-macros/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: pluginagentmarketplace (https://skillmd.com/u/pluginagentmarketplace)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/pluginagentmarketplace/rust-macros

---


# Rust Macros Skill

Master Rust's macro system: declarative macros (macro_rules!) and procedural macros.

## Quick Start

### Declarative Macros

```rust
macro_rules! vec_of_strings {
    ($($x:expr),* $(,)?) => {
        vec![$($x.to_string()),*]
    };
}

let v = vec_of_strings!["a", "b", "c"];
```

### Fragment Specifiers

| Specifier | Matches |
|-----------|---------|
| `ident` | Identifier |
| `expr` | Expression |
| `ty` | Type |
| `pat` | Pattern |
| `tt` | Token tree |
| `literal` | Literal |

### Repetition

```rust
macro_rules! hashmap {
    ($($key:expr => $value:expr),* $(,)?) => {{
        let mut map = std::collections::HashMap::new();
        $(map.insert($key, $value);)*
        map
    }};
}

let m = hashmap! { "one" => 1, "two" => 2 };
```

## Procedural Macros

```toml
[lib]
proc-macro = true

[dependencies]
syn = { version = "2", features = ["full"] }
quote = "1"
```

### Derive Macro

```rust
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, DeriveInput};

#[proc_macro_derive(HelloMacro)]
pub fn hello_derive(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    let name = input.ident;

    quote! {
        impl HelloMacro for #name {
            fn hello() {
                println!("Hello from {}!", stringify!(#name));
            }
        }
    }.into()
}
```

## Debugging

```bash
cargo expand              # Expand all macros
cargo expand main         # Expand specific
```

## Troubleshooting

| Problem | Solution |
|---------|----------|
| Hygiene issues | Use `$crate::` |
| Order matters | Define before use |

## Resources

- [The Little Book of Rust Macros](https://veykril.github.io/tlborm/)
- [syn docs](https://docs.rs/syn)

