pluginagentmarketplace/custom-plugin-rust

rust-macros

Master Rust macros - declarative and procedural macros

Vedi sorgente

Come usare

  1. Copia il comando e controlla il pacchetto prima di installarlo nel tuo ambiente agente.
  2. Controlla strumenti, credenziali e ambienti richiesti nel documento originale.
  3. Descrivi input e risultato desiderato; confronta l’output con le fonti.
npx skills add https://github.com/pluginagentmarketplace/custom-plugin-rust --skill rust-macros

Prima di iniziare

  • L’installazione non include abbonamenti, quote API o accesso ai file.
  • Stelle e installazioni indicano attività, non un test indipendente di qualità o sicurezza.
Documento Skill originale

Contenuto dal repository con titoli, esempi, codice, tabelle, link e immagini preservati.

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

SpecifierMatches
identIdentifier
exprExpression
tyType
patPattern
ttToken tree
literalLiteral

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

ProblemSolution
Hygiene issuesUse $crate::
Order mattersDefine before use

Resources

Correlati per attività