rust-macros
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseRust Macros Skill
Rust 宏 Skill
Master Rust's macro system: declarative macros (macro_rules!) and procedural macros.
掌握 Rust 的宏系统:声明式宏(macro_rules!)和过程式宏。
Quick Start
快速开始
Declarative Macros
声明式宏
rust
macro_rules! vec_of_strings {
($($x:expr),* $(,)?) => {
vec![$($x.to_string()),*]
};
}
let v = vec_of_strings!["a", "b", "c"];rust
macro_rules! vec_of_strings {
($($x:expr),* $(,)?) => {
vec![$($x.to_string()),*]
};
}
let v = vec_of_strings!["a", "b", "c"];Fragment Specifiers
片段说明符
| Specifier | Matches |
|---|---|
| Identifier |
| Expression |
| Type |
| Pattern |
| Token tree |
| Literal |
| 说明符 | 匹配内容 |
|---|---|
| 标识符 |
| 表达式 |
| 类型 |
| 模式 |
| Token 树 |
| 字面量 |
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 };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"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()
}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 specificbash
cargo expand # Expand all macros
cargo expand main # Expand specificTroubleshooting
故障排查
| Problem | Solution |
|---|---|
| Hygiene issues | Use |
| Order matters | Define before use |
| 问题 | 解决方案 |
|---|---|
| 卫生性问题 | 使用 |
| 顺序很重要 | 先定义后使用 |