Once you get the grasp of using wrapper types, you can generalize over any kind of comparison function, if you so wish (granted, this would ideally be provided by some library, although as youc an see it can be written by hand):
use lib::BinaryHeapWithCustomComparator;
#[derive(Clone, Debug)]
struct Person {
id: u32,
name: String,
height: u32,
}
fn main ()
{
let p1 = Person {
id: 1,
name: "p1".into(),
height: 42,
};
let p2 = Person {
id: 2,
name: "p2".into(),
height: 27,
};
let p3 = Person {
id: 3,
name: "p3".into(),
height: 0,
};
let mut heap = BinaryHeapWithCustomComparator::new(
|p1: &'_ Person, p2: &'_ Person| {
::core::cmp::Ord::cmp(&p1.height, &p2.height)
}
);
heap.push(p1.clone());
heap.push(p2.clone());
heap.push(p3.clone());
eprintln!("Tallest person: {:?}", heap.peek().unwrap());
let mut heap = BinaryHeapWithCustomComparator::<Person, _>::new(|p1, p2| {
::core::cmp::Ord::cmp(&p1.name, &p2.name)
});
heap.push(p1);
heap.push(p2);
heap.push(p3);
eprintln!("By name, the last person is: {:?}", heap.peek().unwrap());
}