TryMinMax

Trait TryMinMax 

Source
pub trait TryMinMax<T> {
    // Required method
    fn try_select_by<F>(
        self,
        compare: F,
        target: Ordering,
    ) -> OrderResult<Option<T>>
       where F: FnMut(&T, &T) -> Option<Ordering>;

    // Provided methods
    fn try_min(self) -> OrderResult<Option<T>>
       where T: PartialOrd<T>,
             Self: Sized { ... }
    fn try_min_by<F>(self, compare: F) -> OrderResult<Option<T>>
       where F: FnMut(&T, &T) -> Option<Ordering>,
             Self: Sized { ... }
    fn try_min_by_key<K, F>(self, f: F) -> OrderResult<Option<T>>
       where F: FnMut(&T) -> Option<K>,
             K: PartialOrd<K>,
             Self: Sized { ... }
    fn try_max(self) -> OrderResult<Option<T>>
       where T: PartialOrd<T>,
             Self: Sized { ... }
    fn try_max_by<F>(self, compare: F) -> OrderResult<Option<T>>
       where F: FnMut(&T, &T) -> Option<Ordering>,
             Self: Sized { ... }
    fn try_max_by_key<K, F>(self, f: F) -> OrderResult<Option<T>>
       where F: FnMut(&T) -> Option<K>,
             K: PartialOrd<K>,
             Self: Sized { ... }
}
Expand description

Min and max methods for PartialOrd

use try_partialord::*;
use rand::distributions::Standard;
use rand::prelude::*;

let rng = thread_rng();
let mut v: Vec<f32> = Standard.sample_iter(rng).take(100).collect();
let min = v.iter().try_min().unwrap();
assert_eq!(min, v.iter().min_by(|a, b| a.partial_cmp(b).unwrap()));

// min is error because of uncompareabale value `NAN`
v.push(f32::NAN);
let min = v.iter().try_min();
assert!(min.is_err());

Required Methods§

Source

fn try_select_by<F>( self, compare: F, target: Ordering, ) -> OrderResult<Option<T>>
where F: FnMut(&T, &T) -> Option<Ordering>,

Base method for getting min or max. target is to tell what you want to get is min or max.

Provided Methods§

Source

fn try_min(self) -> OrderResult<Option<T>>
where T: PartialOrd<T>, Self: Sized,

PartialOrd version for Iterator::min.

Source

fn try_min_by<F>(self, compare: F) -> OrderResult<Option<T>>
where F: FnMut(&T, &T) -> Option<Ordering>, Self: Sized,

PartialOrd version for Iterator::min_by.

Source

fn try_min_by_key<K, F>(self, f: F) -> OrderResult<Option<T>>
where F: FnMut(&T) -> Option<K>, K: PartialOrd<K>, Self: Sized,

PartialOrd version for Iterator::min_by_key.

Source

fn try_max(self) -> OrderResult<Option<T>>
where T: PartialOrd<T>, Self: Sized,

PartialOrd version for Iterator::max.

Source

fn try_max_by<F>(self, compare: F) -> OrderResult<Option<T>>
where F: FnMut(&T, &T) -> Option<Ordering>, Self: Sized,

PartialOrd version for Iterator::max_by.

Source

fn try_max_by_key<K, F>(self, f: F) -> OrderResult<Option<T>>
where F: FnMut(&T) -> Option<K>, K: PartialOrd<K>, Self: Sized,

PartialOrd version for Iterator::max_by_key.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T, Iter> TryMinMax<T> for Iter
where Iter: IntoIterator<Item = T>,