IteratorExt

Trait IteratorExt 

Source
pub trait IteratorExt: Iterator {
    // Required methods
    fn consume(self) -> Self
       where Self: Sized;
    fn drop(self, n: isize) -> Self
       where Self: Sized + DoubleEndedIterator;
    fn first(self) -> Option<Self::Item>
       where Self: Sized;
    fn first_result(self) -> RvResult<Self::Item>
       where Self: Sized;
    fn last_result(self) -> RvResult<Self::Item>
       where Self: Sized;
    fn single(self) -> RvResult<Self::Item>
       where Self: Sized;
    fn slice(self, left: isize, right: isize) -> Self
       where Self: Sized + Clone + DoubleEndedIterator;
    fn some(self) -> bool
       where Self: Sized;
}
Expand description

Provides extensions for the Iterator trait

Required Methods§

Source

fn consume(self) -> Self
where Self: Sized,

Consume the entire iterator eagerly up until but not including the last call to get None. Allows caller to then call next and get None.

§Examples
use rivia::prelude::*;

assert_eq!(vec![0, 1, 2].into_iter().consume().next(), None);
Source

fn drop(self, n: isize) -> Self
where Self: Sized + DoubleEndedIterator,

Drop the first n items if positive from the iterator eagerly and then return the iterator. Drop the last |n| items if negative from the iterator eagerly and then return the iterator.

§Examples
use rivia::prelude::*;

assert_iter_eq(vec![1, 2, 3].into_iter().drop(1), vec![2, 3]);
assert_iter_eq(vec![1, 2, 3].into_iter().drop(-1), vec![1, 2]);
Source

fn first(self) -> Option<Self::Item>
where Self: Sized,

Returns the first element of the iterator. Alias to nth(0).

first() will return None if n is greater than or equal to the length of the iterator.

§Examples
use rivia::prelude::*;

assert_eq!((0..10).filter(|&x| x == 2).first().unwrap(), 2);
Source

fn first_result(self) -> RvResult<Self::Item>
where Self: Sized,

If the iterator yields at least one element, the first element will be returned, otherwise an error will be returned.

§Examples
use rivia::prelude::*;

assert_eq!((0..10).filter(|&x| x == 2).first().unwrap(), 2);
Source

fn last_result(self) -> RvResult<Self::Item>
where Self: Sized,

If the iterator yields at least one element, the last element will be returned, otherwise an error will be returned.

§Examples
use rivia::prelude::*;

assert_eq!((0..10).filter(|&x| x == 2).last().unwrap(), 2);
Source

fn single(self) -> RvResult<Self::Item>
where Self: Sized,

If the iterator yields a single element, that element will be returned, otherwise an error will be returned.

§Examples
use rivia::prelude::*;

assert_eq!((0..10).filter(|&x| x == 2).single().unwrap(), 2);
Source

fn slice(self, left: isize, right: isize) -> Self

Slice returns this iterator eagerly to only iterate over the range of elements called out by the given indices. Allows for negative notation.

Note this operation uses count() to determine length which means cost O(n) out of the gate.

§Examples
use rivia::prelude::*;

let mut iter = vec![0, 1, 2].into_iter().slice(0, 0);
assert_eq!(iter.next(), Some(0));
assert_eq!(iter.next(), None);

let mut iter = vec![0, 1, 2].into_iter().slice(-1, -1);
assert_eq!(iter.next(), Some(2));
assert_eq!(iter.next(), None);

let mut iter = vec![0, 1, 2].into_iter().slice(-2, -1);
assert_eq!(iter.next(), Some(1));
assert_eq!(iter.next(), Some(2));
assert_eq!(iter.next(), None);
Source

fn some(self) -> bool
where Self: Sized,

If the iterator yields at least one element, true will be returned else false

§Examples
use rivia::prelude::*;

assert_eq!((0..10).filter(|&x| x == 2).some(), true);

Implementors§

Source§

impl<T> IteratorExt for T
where T: Iterator + ?Sized,