使用方法
- 复制安装命令,在自己的智能体环境安装前先检查源码包。
- 从原始文档核对所需工具、凭据和支持的宿主。
- 说明输入材料与期望交付物,并对照来源检查输出。
npx skills add https://github.com/pluginagentmarketplace/custom-plugin-rust --skill rust-macros开始前需了解
- 安装不包含模型订阅、API 额度或访问文件的权限。
- 仓库星数和安装量反映活跃情况,不等于独立质量或安全测试。
仓库原始内容
按源仓库内容呈现,保留标题、案例、代码、表格、链接以及原文引用的演示图片。
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 specificTroubleshooting
| Problem | Solution |
|---|---|
| Hygiene issues | Use $crate:: |
| Order matters | Define before use |

