rust-macros

Master Rust macros - declarative and procedural macros

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "rust-macros" with this command: npx skills add pluginagentmarketplace/custom-plugin-rust/pluginagentmarketplace-custom-plugin-rust-rust-macros

Rust Macros Skill

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

Quick Start

Declarative Macros

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

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

Fragment Specifiers

SpecifierMatches
identIdentifier
exprExpression
tyType
patPattern
ttToken tree
literalLiteral

Repetition

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

[lib]
proc-macro = true

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

Derive Macro

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

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

Troubleshooting

ProblemSolution
Hygiene issuesUse $crate::
Order mattersDefine before use

Resources

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

Automation

error-handling

No summary provided by upstream source.

Repository SourceNeeds Review
Automation

rust-wasm

No summary provided by upstream source.

Repository SourceNeeds Review
Automation

rust-performance

No summary provided by upstream source.

Repository SourceNeeds Review
Automation

rust-docker

No summary provided by upstream source.

Repository SourceNeeds Review