I am trying to write a Rust program that puts integer vectors into a priority queue. The problem is, the default ordering of vectors is lexicographic, and I have to order vectors by their lengths. An ugly solution is to create a new vector struct called VEC
, implement Ord
by length, and just work with VEC
instead. But if possible I'd prefer to override the default Ord
by lex order.
//ugly solution
#[derive(Eq)]
pub struct VEC<T: Eq> {
data: Vec<T>,
}
impl<T: Eq> Ord for VEC<T> {
fn cmp(&self, other: &Self) -> Ordering {
let x = self.data.len();
let y = other.data.len();
y.cmp(&x)
}
}
impl<T: Eq> PartialOrd for VEC<T> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl<T: Eq> PartialEq for VEC<T> {
fn eq(&self, other: &Self) -> bool {
self.data.len() == other.data.len()
}
}