I'm trying to use std::collections::BinaryHeap
with a custom struct. In order to do so, I have to have my struct implement the std::cmp::Ord
trait, but what I need is 2 BinaryHeap
s of the same structs but ordered differently.
Is there a way to define 2 Ord implementations & pick which Ord
gets used, or some other way to specify an alternate ordering?
I think I could define 2 different wrapping structs which keep a reference to the original custom struct and have an implementation of Ord
for each of them, but it seems quite wasteful to have to construct potentially a lot of instances of such wrapping structs.
In Pyhton/Java I'd provide a sorting function/Comparator, but there seems no facility like that. In Scala, I can define a compile-time only type to select the right implicit Ordering implementation; it feels like Rust supports something similar, but I haven't been able to work it out.