std-type
Ordered collections — custom composite keys
Derive comparison traits together for an ordinary composite key. Fields are compared in declaration order. A tie-breaker keeps distinct records with the same primary rank separate. Equal ordering must agree with equality; do not mutate a stored key in a way that changes its relative ordering. Expected stdout (not executed): ["build", "test", "review"] Common mistake: Comparing by priority alone when IDs should distinguish jobs.
Formal Syntax & Structure
rust syntaxrust
#[derive(PartialEq, Eq, PartialOrd, Ord)] struct Key { ... }Working Example
Example Snippetrust
#[derive(PartialEq, Eq, PartialOrd, Ord)]struct Key { priority: u32, id: u32 }fn main() { use std::collections::BTreeMap; let jobs = BTreeMap::from([(Key { priority: 2, id: 8 }, "review"), (Key { priority: 1, id: 9 }, "build"), (Key { priority: 2, id: 3 }, "test")]); println!("{:?}", jobs.values().copied().collect::<Vec<_>>());}