Trait std::iter::OrdIterator
pub trait OrdIterator<A> { fn max(&mut self) -> Option<A>; fn min(&mut self) -> Option<A>; fn min_max(&mut self) -> MinMaxResult<A>; }
A trait for iterators over elements which can be compared to one another.
The type of each element must ascribe to the Ord
trait.
Required Methods
fn max(&mut self) -> Option<A>
Consumes the entire iterator to return the maximum element.
Example
let a = [1, 2, 3, 4, 5]; assert!(a.iter().max().unwrap() == &5);
fn min(&mut self) -> Option<A>
Consumes the entire iterator to return the minimum element.
Example
let a = [1, 2, 3, 4, 5]; assert!(a.iter().min().unwrap() == &1);
fn min_max(&mut self) -> MinMaxResult<A>
min_max
finds the minimum and maximum elements in the iterator.
The return type MinMaxResult
is an enum of three variants:
- NoElements
if the iterator is empty.
- OneElement(x)
if the iterator has exactly one element.
- MinMax(x, y)
is returned otherwise, where x <= y
. Two values are equal if and only if
there is more than one element in the iterator and all elements are equal.
On an iterator of length n
, min_max
does 1.5 * n
comparisons,
and so faster than calling min
and max separately which does
2 * n` comparisons.
Example
use std::iter::{NoElements, OneElement, MinMax}; let v: [int, ..0] = []; assert_eq!(v.iter().min_max(), NoElements); let v = [1i]; assert!(v.iter().min_max() == OneElement(&1)); let v = [1i, 2, 3, 4, 5]; assert!(v.iter().min_max() == MinMax(&1, &5)); let v = [1i, 2, 3, 4, 5, 6]; assert!(v.iter().min_max() == MinMax(&1, &6)); let v = [1i, 1, 1, 1]; assert!(v.iter().min_max() == MinMax(&1, &1));
Implementors
impl<A: TotalOrd, T: Iterator<A>> OrdIterator<A> for T