Trait std::iter::IteratorExtStable [-] [+] [src]

pub trait IteratorExt: Iterator where Self: Iterator {
    fn count(self) -> usize { ... }
    fn last(self) -> Option<<Self as Iterator>::Item> { ... }
    fn nth(&mut self, n: usize) -> Option<<Self as Iterator>::Item> { ... }
    fn chain<U>(self, other: U) -> Chain<Self, U> where U: Iterator, <U as Iterator>::Item == <Self as Iterator>::Item { ... }
    fn zip<U>(self, other: U) -> Zip<Self, U> where U: Iterator { ... }
    fn map<B, F>(self, f: F) -> Map<Self, F> where F: FnMut(<Self as Iterator>::Item), <F as FnMut(<Self as Iterator>::Item)>::Output == B { ... }
    fn filter<P>(self, predicate: P) -> Filter<Self, P> where P: FnMut(&<Self as Iterator>::Item), <P as FnMut(&<Self as Iterator>::Item)>::Output == bool { ... }
    fn filter_map<B, F>(self, f: F) -> FilterMap<Self, F> where F: FnMut(<Self as Iterator>::Item), <F as FnMut(<Self as Iterator>::Item)>::Output == Option<B> { ... }
    fn enumerate(self) -> Enumerate<Self> { ... }
    fn peekable(self) -> Peekable<Self> { ... }
    fn skip_while<P>(self, predicate: P) -> SkipWhile<Self, P> where P: FnMut(&<Self as Iterator>::Item), <P as FnMut(&<Self as Iterator>::Item)>::Output == bool { ... }
    fn take_while<P>(self, predicate: P) -> TakeWhile<Self, P> where P: FnMut(&<Self as Iterator>::Item), <P as FnMut(&<Self as Iterator>::Item)>::Output == bool { ... }
    fn skip(self, n: usize) -> Skip<Self> { ... }
    fn take(self, n: usize) -> Take<Self> { ... }
    fn scan<St, B, F>(self, initial_state: St, f: F) -> Scan<Self, St, F> where F: FnMut(&mut St, <Self as Iterator>::Item), <F as FnMut(&mut St, <Self as Iterator>::Item)>::Output == Option<B> { ... }
    fn flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F> where U: Iterator, F: FnMut(<Self as Iterator>::Item), <F as FnMut(<Self as Iterator>::Item)>::Output == U { ... }
    fn fuse(self) -> Fuse<Self> { ... }
    fn inspect<F>(self, f: F) -> Inspect<Self, F> where F: FnMut(&<Self as Iterator>::Item), <F as FnMut(&<Self as Iterator>::Item)>::Output == () { ... }
    fn by_ref(&mut self) -> &mut Self { ... }
    fn collect<B>(self) -> B where B: FromIterator<<Self as Iterator>::Item> { ... }
    fn partition<B, F>(self, f: F) -> (B, B) where B: Default, B: Extend<<Self as Iterator>::Item>, F: FnMut(&<Self as Iterator>::Item), <F as FnMut(&<Self as Iterator>::Item)>::Output == bool { ... }
    fn fold<B, F>(self, init: B, f: F) -> B where F: FnMut(B, <Self as Iterator>::Item), <F as FnMut(B, <Self as Iterator>::Item)>::Output == B { ... }
    fn all<F>(self, f: F) -> bool where F: FnMut(<Self as Iterator>::Item), <F as FnMut(<Self as Iterator>::Item)>::Output == bool { ... }
    fn any<F>(&mut self, f: F) -> bool where F: FnMut(<Self as Iterator>::Item), <F as FnMut(<Self as Iterator>::Item)>::Output == bool { ... }
    fn find<P>(&mut self, predicate: P) -> Option<<Self as Iterator>::Item> where P: FnMut(&<Self as Iterator>::Item), <P as FnMut(&<Self as Iterator>::Item)>::Output == bool { ... }
    fn position<P>(&mut self, predicate: P) -> Option<usize> where P: FnMut(<Self as Iterator>::Item), <P as FnMut(<Self as Iterator>::Item)>::Output == bool { ... }
    fn rposition<P>(&mut self, predicate: P) -> Option<usize> where P: FnMut(<Self as Iterator>::Item), <P as FnMut(<Self as Iterator>::Item)>::Output == bool, Self: ExactSizeIterator, Self: DoubleEndedIterator { ... }
    fn max(self) -> Option<<Self as Iterator>::Item> where <Self as Iterator>::Item: Ord { ... }
    fn min(self) -> Option<<Self as Iterator>::Item> where <Self as Iterator>::Item: Ord { ... }
    fn min_max(self) -> MinMaxResult<<Self as Iterator>::Item> where <Self as Iterator>::Item: Ord { ... }
    fn max_by<B, F>(self, f: F) -> Option<<Self as Iterator>::Item> where B: Ord, F: FnMut(&<Self as Iterator>::Item), <F as FnMut(&<Self as Iterator>::Item)>::Output == B { ... }
    fn min_by<B, F>(self, f: F) -> Option<<Self as Iterator>::Item> where B: Ord, F: FnMut(&<Self as Iterator>::Item), <F as FnMut(&<Self as Iterator>::Item)>::Output == B { ... }
    fn rev(self) -> Rev<Self> { ... }
    fn unzip<A, B, FromA, FromB>(self) -> (FromA, FromB) where FromA: Default, FromA: Extend<A>, FromB: Default, FromB: Extend<B>, Self: Iterator, <Self as Iterator>::Item == (A, B) { ... }
    fn cloned(self) -> Cloned<Self> where <Self as Iterator>::Item: Deref, <<Self as Iterator>::Item as Deref>::Output: Clone { ... }
    fn cycle(self) -> Cycle<Self> where Self: Clone { ... }
    fn reverse_in_place<'a, T>(&mut self) where T: 'a, Self: Iterator, <Self as Iterator>::Item == &'a mut T, Self: DoubleEndedIterator { ... }
}

An extension trait providing numerous methods applicable to all iterators.

Provided Methods

fn count(self) -> usize

Counts the number of elements in this iterator.

Examples

fn main() { let a = [1, 2, 3, 4, 5]; assert_eq!(a.iter().count(), 5); }
let a = [1, 2, 3, 4, 5];
assert_eq!(a.iter().count(), 5);

fn last(self) -> Option<<Self as Iterator>::Item>

Loops through the entire iterator, returning the last element of the iterator.

Examples

fn main() { let a = [1, 2, 3, 4, 5]; assert!(a.iter().last().unwrap() == &5); }
let a = [1, 2, 3, 4, 5];
assert!(a.iter().last().unwrap() == &5);

fn nth(&mut self, n: usize) -> Option<<Self as Iterator>::Item>

Loops through n iterations, returning the nth element of the iterator.

Examples

fn main() { let a = [1, 2, 3, 4, 5]; let mut it = a.iter(); assert!(it.nth(2).unwrap() == &3); assert!(it.nth(2) == None); }
let a = [1, 2, 3, 4, 5];
let mut it = a.iter();
assert!(it.nth(2).unwrap() == &3);
assert!(it.nth(2) == None);

fn chain<U>(self, other: U) -> Chain<Self, U> where U: Iterator, <U as Iterator>::Item == <Self as Iterator>::Item

Chain this iterator with another, returning a new iterator that will finish iterating over the current iterator, and then iterate over the other specified iterator.

Examples

fn main() { let a = [0]; let b = [1]; let mut it = a.iter().chain(b.iter()); assert_eq!(it.next().unwrap(), &0); assert_eq!(it.next().unwrap(), &1); assert!(it.next().is_none()); }
let a = [0];
let b = [1];
let mut it = a.iter().chain(b.iter());
assert_eq!(it.next().unwrap(), &0);
assert_eq!(it.next().unwrap(), &1);
assert!(it.next().is_none());

fn zip<U>(self, other: U) -> Zip<Self, U> where U: Iterator

Creates an iterator that iterates over both this and the specified iterators simultaneously, yielding the two elements as pairs. When either iterator returns None, all further invocations of next() will return None.

Examples

fn main() { let a = [0]; let b = [1]; let mut it = a.iter().zip(b.iter()); assert_eq!(it.next().unwrap(), (&0, &1)); assert!(it.next().is_none()); }
let a = [0];
let b = [1];
let mut it = a.iter().zip(b.iter());
assert_eq!(it.next().unwrap(), (&0, &1));
assert!(it.next().is_none());

fn map<B, F>(self, f: F) -> Map<Self, F> where F: FnMut(<Self as Iterator>::Item), <F as FnMut(<Self as Iterator>::Item)>::Output == B

Creates a new iterator that will apply the specified function to each element returned by the first, yielding the mapped element instead.

Examples

fn main() { let a = [1, 2]; let mut it = a.iter().map(|&x| 2 * x); assert_eq!(it.next().unwrap(), 2); assert_eq!(it.next().unwrap(), 4); assert!(it.next().is_none()); }
let a = [1, 2];
let mut it = a.iter().map(|&x| 2 * x);
assert_eq!(it.next().unwrap(), 2);
assert_eq!(it.next().unwrap(), 4);
assert!(it.next().is_none());

fn filter<P>(self, predicate: P) -> Filter<Self, P> where P: FnMut(&<Self as Iterator>::Item), <P as FnMut(&<Self as Iterator>::Item)>::Output == bool

Creates an iterator that applies the predicate to each element returned by this iterator. The only elements that will be yielded are those that make the predicate evaluate to true.

Examples

fn main() { let a = [1, 2]; let mut it = a.iter().filter(|&x| *x > 1); assert_eq!(it.next().unwrap(), &2); assert!(it.next().is_none()); }
let a = [1, 2];
let mut it = a.iter().filter(|&x| *x > 1);
assert_eq!(it.next().unwrap(), &2);
assert!(it.next().is_none());

fn filter_map<B, F>(self, f: F) -> FilterMap<Self, F> where F: FnMut(<Self as Iterator>::Item), <F as FnMut(<Self as Iterator>::Item)>::Output == Option<B>

Creates an iterator that both filters and maps elements. If the specified function returns None, the element is skipped. Otherwise the option is unwrapped and the new value is yielded.

Examples

fn main() { let a = [1, 2]; let mut it = a.iter().filter_map(|&x| if x > 1 {Some(2 * x)} else {None}); assert_eq!(it.next().unwrap(), 4); assert!(it.next().is_none()); }
let a = [1, 2];
let mut it = a.iter().filter_map(|&x| if x > 1 {Some(2 * x)} else {None});
assert_eq!(it.next().unwrap(), 4);
assert!(it.next().is_none());

fn enumerate(self) -> Enumerate<Self>

Creates an iterator that yields a pair of the value returned by this iterator plus the current index of iteration.

Examples

fn main() { let a = [100, 200]; let mut it = a.iter().enumerate(); assert_eq!(it.next().unwrap(), (0, &100)); assert_eq!(it.next().unwrap(), (1, &200)); assert!(it.next().is_none()); }
let a = [100, 200];
let mut it = a.iter().enumerate();
assert_eq!(it.next().unwrap(), (0, &100));
assert_eq!(it.next().unwrap(), (1, &200));
assert!(it.next().is_none());

fn peekable(self) -> Peekable<Self>

Creates an iterator that has a .peek() method that returns an optional reference to the next element.

Examples

fn main() { let xs = [100, 200, 300]; let mut it = xs.iter().cloned().peekable(); assert_eq!(*it.peek().unwrap(), 100); assert_eq!(it.next().unwrap(), 100); assert_eq!(it.next().unwrap(), 200); assert_eq!(*it.peek().unwrap(), 300); assert_eq!(*it.peek().unwrap(), 300); assert_eq!(it.next().unwrap(), 300); assert!(it.peek().is_none()); assert!(it.next().is_none()); }
let xs = [100, 200, 300];
let mut it = xs.iter().cloned().peekable();
assert_eq!(*it.peek().unwrap(), 100);
assert_eq!(it.next().unwrap(), 100);
assert_eq!(it.next().unwrap(), 200);
assert_eq!(*it.peek().unwrap(), 300);
assert_eq!(*it.peek().unwrap(), 300);
assert_eq!(it.next().unwrap(), 300);
assert!(it.peek().is_none());
assert!(it.next().is_none());

fn skip_while<P>(self, predicate: P) -> SkipWhile<Self, P> where P: FnMut(&<Self as Iterator>::Item), <P as FnMut(&<Self as Iterator>::Item)>::Output == bool

Creates an iterator that invokes the predicate on elements until it returns false. Once the predicate returns false, that element and all further elements are yielded.

Examples

fn main() { let a = [1, 2, 3, 4, 5]; let mut it = a.iter().skip_while(|&a| *a < 3); assert_eq!(it.next().unwrap(), &3); assert_eq!(it.next().unwrap(), &4); assert_eq!(it.next().unwrap(), &5); assert!(it.next().is_none()); }
let a = [1, 2, 3, 4, 5];
let mut it = a.iter().skip_while(|&a| *a < 3);
assert_eq!(it.next().unwrap(), &3);
assert_eq!(it.next().unwrap(), &4);
assert_eq!(it.next().unwrap(), &5);
assert!(it.next().is_none());

fn take_while<P>(self, predicate: P) -> TakeWhile<Self, P> where P: FnMut(&<Self as Iterator>::Item), <P as FnMut(&<Self as Iterator>::Item)>::Output == bool

Creates an iterator that yields elements so long as the predicate returns true. After the predicate returns false for the first time, no further elements will be yielded.

Examples

fn main() { let a = [1, 2, 3, 4, 5]; let mut it = a.iter().take_while(|&a| *a < 3); assert_eq!(it.next().unwrap(), &1); assert_eq!(it.next().unwrap(), &2); assert!(it.next().is_none()); }
let a = [1, 2, 3, 4, 5];
let mut it = a.iter().take_while(|&a| *a < 3);
assert_eq!(it.next().unwrap(), &1);
assert_eq!(it.next().unwrap(), &2);
assert!(it.next().is_none());

fn skip(self, n: usize) -> Skip<Self>

Creates an iterator that skips the first n elements of this iterator, and then yields all further items.

Examples

fn main() { let a = [1, 2, 3, 4, 5]; let mut it = a.iter().skip(3); assert_eq!(it.next().unwrap(), &4); assert_eq!(it.next().unwrap(), &5); assert!(it.next().is_none()); }
let a = [1, 2, 3, 4, 5];
let mut it = a.iter().skip(3);
assert_eq!(it.next().unwrap(), &4);
assert_eq!(it.next().unwrap(), &5);
assert!(it.next().is_none());

fn take(self, n: usize) -> Take<Self>

Creates an iterator that yields the first n elements of this iterator.

Examples

fn main() { let a = [1, 2, 3, 4, 5]; let mut it = a.iter().take(3); assert_eq!(it.next().unwrap(), &1); assert_eq!(it.next().unwrap(), &2); assert_eq!(it.next().unwrap(), &3); assert!(it.next().is_none()); }
let a = [1, 2, 3, 4, 5];
let mut it = a.iter().take(3);
assert_eq!(it.next().unwrap(), &1);
assert_eq!(it.next().unwrap(), &2);
assert_eq!(it.next().unwrap(), &3);
assert!(it.next().is_none());

fn scan<St, B, F>(self, initial_state: St, f: F) -> Scan<Self, St, F> where F: FnMut(&mut St, <Self as Iterator>::Item), <F as FnMut(&mut St, <Self as Iterator>::Item)>::Output == Option<B>

Creates a new iterator that behaves in a similar fashion to fold. There is a state which is passed between each iteration and can be mutated as necessary. The yielded values from the closure are yielded from the Scan instance when not None.

Examples

fn main() { let a = [1, 2, 3, 4, 5]; let mut it = a.iter().scan(1, |fac, &x| { *fac = *fac * x; Some(*fac) }); assert_eq!(it.next().unwrap(), 1); assert_eq!(it.next().unwrap(), 2); assert_eq!(it.next().unwrap(), 6); assert_eq!(it.next().unwrap(), 24); assert_eq!(it.next().unwrap(), 120); assert!(it.next().is_none()); }
let a = [1, 2, 3, 4, 5];
let mut it = a.iter().scan(1, |fac, &x| {
  *fac = *fac * x;
  Some(*fac)
});
assert_eq!(it.next().unwrap(), 1);
assert_eq!(it.next().unwrap(), 2);
assert_eq!(it.next().unwrap(), 6);
assert_eq!(it.next().unwrap(), 24);
assert_eq!(it.next().unwrap(), 120);
assert!(it.next().is_none());

fn flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F> where U: Iterator, F: FnMut(<Self as Iterator>::Item), <F as FnMut(<Self as Iterator>::Item)>::Output == U

Creates an iterator that maps each element to an iterator, and yields the elements of the produced iterators.

Examples

fn main() { let xs = [2, 3]; let ys = [0, 1, 0, 1, 2]; let it = xs.iter().flat_map(|&x| std::iter::count(0, 1).take(x)); // Check that `it` has the same elements as `ys` for (i, x) in it.enumerate() { assert_eq!(x, ys[i]); } }
let xs = [2, 3];
let ys = [0, 1, 0, 1, 2];
let it = xs.iter().flat_map(|&x| std::iter::count(0, 1).take(x));
// Check that `it` has the same elements as `ys`
for (i, x) in it.enumerate() {
    assert_eq!(x, ys[i]);
}

fn fuse(self) -> Fuse<Self>

Creates an iterator that yields None forever after the underlying iterator yields None. Random-access iterator behavior is not affected, only single and double-ended iterator behavior.

Examples

fn main() { fn process<U: Iterator<Item=isize>>(it: U) -> isize { let mut it = it.fuse(); let mut sum = 0; for x in it.by_ref() { if x > 5 { break; } sum += x; } // did we exhaust the iterator? if it.next().is_none() { sum += 1000; } sum } let x = vec![1, 2, 3, 7, 8, 9]; assert_eq!(process(x.into_iter()), 6); let x = vec![1, 2, 3]; assert_eq!(process(x.into_iter()), 1006); }
fn process<U: Iterator<Item=isize>>(it: U) -> isize {
    let mut it = it.fuse();
    let mut sum = 0;
    for x in it.by_ref() {
        if x > 5 {
            break;
        }
        sum += x;
    }
    // did we exhaust the iterator?
    if it.next().is_none() {
        sum += 1000;
    }
    sum
}
let x = vec![1, 2, 3, 7, 8, 9];
assert_eq!(process(x.into_iter()), 6);
let x = vec![1, 2, 3];
assert_eq!(process(x.into_iter()), 1006);

fn inspect<F>(self, f: F) -> Inspect<Self, F> where F: FnMut(&<Self as Iterator>::Item), <F as FnMut(&<Self as Iterator>::Item)>::Output == ()

Creates an iterator that calls a function with a reference to each element before yielding it. This is often useful for debugging an iterator pipeline.

Examples

fn main() { use std::iter::AdditiveIterator; let a = [1, 4, 2, 3, 8, 9, 6]; let sum = a.iter() .map(|x| *x) .inspect(|&x| println!("filtering {}", x)) .filter(|&x| x % 2 == 0) .inspect(|&x| println!("{} made it through", x)) .sum(); println!("{}", sum); }
use std::iter::AdditiveIterator;

let a = [1, 4, 2, 3, 8, 9, 6];
let sum = a.iter()
           .map(|x| *x)
           .inspect(|&x| println!("filtering {}", x))
           .filter(|&x| x % 2 == 0)
           .inspect(|&x| println!("{} made it through", x))
           .sum();
println!("{}", sum);

fn by_ref(&mut self) -> &mut Self

Creates a wrapper around a mutable reference to the iterator.

This is useful to allow applying iterator adaptors while still retaining ownership of the original iterator value.

Examples

fn main() { let mut it = 0..10; // sum the first five values let partial_sum = it.by_ref().take(5).fold(0, |a, b| a + b); assert!(partial_sum == 10); assert!(it.next() == Some(5)); }
let mut it = 0..10;
// sum the first five values
let partial_sum = it.by_ref().take(5).fold(0, |a, b| a + b);
assert!(partial_sum == 10);
assert!(it.next() == Some(5));

fn collect<B>(self) -> B where B: FromIterator<<Self as Iterator>::Item>

Loops through the entire iterator, collecting all of the elements into a container implementing FromIterator.

Examples

fn main() { let a = [1, 2, 3, 4, 5]; let b: Vec<_> = a.iter().cloned().collect(); assert_eq!(a, b); }
let a = [1, 2, 3, 4, 5];
let b: Vec<_> = a.iter().cloned().collect();
assert_eq!(a, b);

fn partition<B, F>(self, f: F) -> (B, B) where B: Default, B: Extend<<Self as Iterator>::Item>, F: FnMut(&<Self as Iterator>::Item), <F as FnMut(&<Self as Iterator>::Item)>::Output == bool

Loops through the entire iterator, collecting all of the elements into one of two containers, depending on a predicate. The elements of the first container satisfy the predicate, while the elements of the second do not.

fn main() { let vec = vec![1, 2, 3, 4]; let (even, odd): (Vec<_>, Vec<_>) = vec.into_iter().partition(|&n| n % 2 == 0); assert_eq!(even, vec![2, 4]); assert_eq!(odd, vec![1, 3]); }
let vec = vec![1, 2, 3, 4];
let (even, odd): (Vec<_>, Vec<_>) = vec.into_iter().partition(|&n| n % 2 == 0);
assert_eq!(even, vec![2, 4]);
assert_eq!(odd, vec![1, 3]);

fn fold<B, F>(self, init: B, f: F) -> B where F: FnMut(B, <Self as Iterator>::Item), <F as FnMut(B, <Self as Iterator>::Item)>::Output == B

Performs a fold operation over the entire iterator, returning the eventual state at the end of the iteration.

Examples

fn main() { let a = [1, 2, 3, 4, 5]; assert!(a.iter().fold(0, |a, &b| a + b) == 15); }
let a = [1, 2, 3, 4, 5];
assert!(a.iter().fold(0, |a, &b| a + b) == 15);

fn all<F>(self, f: F) -> bool where F: FnMut(<Self as Iterator>::Item), <F as FnMut(<Self as Iterator>::Item)>::Output == bool

Tests whether the predicate holds true for all elements in the iterator.

Examples

fn main() { let a = [1, 2, 3, 4, 5]; assert!(a.iter().all(|x| *x > 0)); assert!(!a.iter().all(|x| *x > 2)); }
let a = [1, 2, 3, 4, 5];
assert!(a.iter().all(|x| *x > 0));
assert!(!a.iter().all(|x| *x > 2));

fn any<F>(&mut self, f: F) -> bool where F: FnMut(<Self as Iterator>::Item), <F as FnMut(<Self as Iterator>::Item)>::Output == bool

Tests whether any element of an iterator satisfies the specified predicate.

Does not consume the iterator past the first found element.

Examples

fn main() { let a = [1, 2, 3, 4, 5]; let mut it = a.iter(); assert!(it.any(|x| *x == 3)); assert_eq!(it.as_slice(), [4, 5]); }
let a = [1, 2, 3, 4, 5];
let mut it = a.iter();
assert!(it.any(|x| *x == 3));
assert_eq!(it.as_slice(), [4, 5]);

fn find<P>(&mut self, predicate: P) -> Option<<Self as Iterator>::Item> where P: FnMut(&<Self as Iterator>::Item), <P as FnMut(&<Self as Iterator>::Item)>::Output == bool

Returns the first element satisfying the specified predicate.

Does not consume the iterator past the first found element.

Examples

fn main() { let a = [1, 2, 3, 4, 5]; let mut it = a.iter(); assert_eq!(it.find(|&x| *x == 3).unwrap(), &3); assert_eq!(it.as_slice(), [4, 5]); }
let a = [1, 2, 3, 4, 5];
let mut it = a.iter();
assert_eq!(it.find(|&x| *x == 3).unwrap(), &3);
assert_eq!(it.as_slice(), [4, 5]);

fn position<P>(&mut self, predicate: P) -> Option<usize> where P: FnMut(<Self as Iterator>::Item), <P as FnMut(<Self as Iterator>::Item)>::Output == bool

Return the index of the first element satisfying the specified predicate

Does not consume the iterator past the first found element.

Examples

fn main() { let a = [1, 2, 3, 4, 5]; let mut it = a.iter(); assert_eq!(it.position(|x| *x == 3).unwrap(), 2); assert_eq!(it.as_slice(), [4, 5]); }
let a = [1, 2, 3, 4, 5];
let mut it = a.iter();
assert_eq!(it.position(|x| *x == 3).unwrap(), 2);
assert_eq!(it.as_slice(), [4, 5]);

fn rposition<P>(&mut self, predicate: P) -> Option<usize> where P: FnMut(<Self as Iterator>::Item), <P as FnMut(<Self as Iterator>::Item)>::Output == bool, Self: ExactSizeIterator, Self: DoubleEndedIterator

Return the index of the last element satisfying the specified predicate

If no element matches, None is returned.

Does not consume the iterator before the first found element.

Examples

fn main() { let a = [1, 2, 2, 4, 5]; let mut it = a.iter(); assert_eq!(it.rposition(|x| *x == 2).unwrap(), 2); assert_eq!(it.as_slice(), [1, 2]); }
let a = [1, 2, 2, 4, 5];
let mut it = a.iter();
assert_eq!(it.rposition(|x| *x == 2).unwrap(), 2);
assert_eq!(it.as_slice(), [1, 2]);

fn max(self) -> Option<<Self as Iterator>::Item> where <Self as Iterator>::Item: Ord

Consumes the entire iterator to return the maximum element.

Examples

fn main() { let a = [1, 2, 3, 4, 5]; assert!(a.iter().max().unwrap() == &5); }
let a = [1, 2, 3, 4, 5];
assert!(a.iter().max().unwrap() == &5);

fn min(self) -> Option<<Self as Iterator>::Item> where <Self as Iterator>::Item: Ord

Consumes the entire iterator to return the minimum element.

Examples

fn main() { let a = [1, 2, 3, 4, 5]; assert!(a.iter().min().unwrap() == &1); }
let a = [1, 2, 3, 4, 5];
assert!(a.iter().min().unwrap() == &1);

fn min_max(self) -> MinMaxResult<<Self as Iterator>::Item> where <Self as Iterator>::Item: Ord

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 is faster than calling min and max separately which does 2 * n comparisons.

Examples

fn main() { use std::iter::MinMaxResult::{NoElements, OneElement, MinMax}; let a: [isize; 0] = []; assert_eq!(a.iter().min_max(), NoElements); let a = [1]; assert!(a.iter().min_max() == OneElement(&1)); let a = [1, 2, 3, 4, 5]; assert!(a.iter().min_max() == MinMax(&1, &5)); let a = [1, 1, 1, 1]; assert!(a.iter().min_max() == MinMax(&1, &1)); }
use std::iter::MinMaxResult::{NoElements, OneElement, MinMax};

let a: [isize; 0] = [];
assert_eq!(a.iter().min_max(), NoElements);

let a = [1];
assert!(a.iter().min_max() == OneElement(&1));

let a = [1, 2, 3, 4, 5];
assert!(a.iter().min_max() == MinMax(&1, &5));

let a = [1, 1, 1, 1];
assert!(a.iter().min_max() == MinMax(&1, &1));

fn max_by<B, F>(self, f: F) -> Option<<Self as Iterator>::Item> where B: Ord, F: FnMut(&<Self as Iterator>::Item), <F as FnMut(&<Self as Iterator>::Item)>::Output == B

Return the element that gives the maximum value from the specified function.

Examples

fn main() { use core::num::SignedInt; let a = [-3, 0, 1, 5, -10]; assert_eq!(*a.iter().max_by(|x| x.abs()).unwrap(), -10); }
use core::num::SignedInt;

let a = [-3, 0, 1, 5, -10];
assert_eq!(*a.iter().max_by(|x| x.abs()).unwrap(), -10);

fn min_by<B, F>(self, f: F) -> Option<<Self as Iterator>::Item> where B: Ord, F: FnMut(&<Self as Iterator>::Item), <F as FnMut(&<Self as Iterator>::Item)>::Output == B

Return the element that gives the minimum value from the specified function.

Examples

fn main() { use core::num::SignedInt; let a = [-3, 0, 1, 5, -10]; assert_eq!(*a.iter().min_by(|x| x.abs()).unwrap(), 0); }
use core::num::SignedInt;

let a = [-3, 0, 1, 5, -10];
assert_eq!(*a.iter().min_by(|x| x.abs()).unwrap(), 0);

fn rev(self) -> Rev<Self>

Change the direction of the iterator

The flipped iterator swaps the ends on an iterator that can already be iterated from the front and from the back.

If the iterator also implements RandomAccessIterator, the flipped iterator is also random access, with the indices starting at the back of the original iterator.

Note: Random access with flipped indices still only applies to the first std::usize::MAX elements of the original iterator.

fn unzip<A, B, FromA, FromB>(self) -> (FromA, FromB) where FromA: Default, FromA: Extend<A>, FromB: Default, FromB: Extend<B>, Self: Iterator, <Self as Iterator>::Item == (A, B)

Converts an iterator of pairs into a pair of containers.

Loops through the entire iterator, collecting the first component of each item into one new container, and the second component into another.

Examples

fn main() { let a = [(1, 2), (3, 4)]; let (left, right): (Vec<_>, Vec<_>) = a.iter().cloned().unzip(); assert_eq!([1, 3], left); assert_eq!([2, 4], right); }
let a = [(1, 2), (3, 4)];
let (left, right): (Vec<_>, Vec<_>) = a.iter().cloned().unzip();
assert_eq!([1, 3], left);
assert_eq!([2, 4], right);

fn cloned(self) -> Cloned<Self> where <Self as Iterator>::Item: Deref, <<Self as Iterator>::Item as Deref>::Output: Clone

Creates an iterator that clones the elements it yields. Useful for converting an Iterator<&T> to an Iterator.

fn cycle(self) -> Cycle<Self> where Self: Clone

Repeats an iterator endlessly

Examples

fn main() { let a = [1, 2]; let mut it = a.iter().cycle(); assert_eq!(it.next().unwrap(), &1); assert_eq!(it.next().unwrap(), &2); assert_eq!(it.next().unwrap(), &1); }
let a = [1, 2];
let mut it = a.iter().cycle();
assert_eq!(it.next().unwrap(), &1);
assert_eq!(it.next().unwrap(), &2);
assert_eq!(it.next().unwrap(), &1);

fn reverse_in_place<'a, T>(&mut self) where T: 'a, Self: Iterator, <Self as Iterator>::Item == &'a mut T, Self: DoubleEndedIterator

Use an iterator to reverse a container in place.

Implementors