3 ms·
Why a macro? Why not just define that comparator as a function? fn value_nans_last<T: Float>(a: &T, b: &T) -> Ordering { match (a, b) { (x, y
by twic 12y ago
Why a macro? Why not just define that comparator as a function?
fn value_nans_last<T: Float>(a: &T, b: &T) -> Ordering {
match (a, b) {
(x, y) if x.is_nan() && y.is_nan() => Ordering::Equal,
(x, _) if x.is_nan() => Ordering::Greater,
(_, y) if y.is_nan() => Ordering::Less,
(_, _) => a.partial_cmp(b).unwrap()
}
}
xs.sort_by(value_nans_last);
- Ygg2 12y agoI haven't done sorting, so no knowledge of sort interface in Rust, but maybe so it works across different sortable collections?
- pcwalton 12y agoThere's no need to use a macro: you can abstract over different kinds of sortable collections using a trait. (That said, there's no real reason to generalize IMO: by far the most common case is sorting slices.)