typestar

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

  1. Debug gives {:?}, Clone gives .clone().
  2. PartialEq enables ==; Hash and Eq allow map keys.
  3. Default fills a struct from each field's default.

Keywords and builtins used here

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.

Type this snippet

Step 1 of 3 in Macros, step 7 of 12 in Modules, tests & macros.

← Previous Next →