Derive macros in Rust
The derives worth reaching for, and what each one buys you.
use std::collections::HashSet;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, PartialOrd, Ord)]
struct StepId {
tour: String,
idx: u32,
}
fn main() {
let a = StepId { tour: "traits".to_string(), idx: 0 };
let b = a.clone();
println!("{:?} == {:?}: {}", a, b, a == b);
let mut seen = HashSet::new();
seen.insert(a);
seen.insert(b);
println!("{} unique", seen.len());
println!("{:?}", StepId::default());
}
How it works
Debuggives{:?},Clonegives.clone().PartialEqenables==;HashandEqallow map keys.Defaultfills a struct from each field's default.
Keywords and builtins used here
StepIdStringfnletmainmutstructu32use
The run, in numbers
- Lines
- 19
- Characters to type
- 413
- Tokens
- 123
- Three-star pace
- 110 tpm
At the three-star pace of 110 tokens a minute, this run takes about 67 seconds.
Step 1 of 3 in Macros, step 7 of 12 in Modules, tests & macros.