Let I have two Vec::<T>, named a and b. I want to compare them and get Less or Greater or Equal.
It T implements Ord I can just write a.cmp(&b). But what can I do in opposite case?
Currently I need to write something like this:
for i in 0..min(a.len(), b.len()) {
let compare_elements = // ... some code comparing a[i] and b[i]
if compare_elements != Equal {
return compare_elements;
}
}
return a.len().cmp(&b.len());
I want to have a method cmp_by(rhs: &Vec<T>, F: Fn(T) -> Ordering) with this usage:
a.cmp_by(&b, |lhs, rhs|
// some code comparing lhs and rhs
)
There is an (unfortunately) still unstable Iterator method that gives you (almost) what you are asking for (see this Rust Playground), allowing you to write a.iter().cmp_by(&b, |lhs, rhs| …)