pub trait NonEmptyIterator: IntoIterator {
Show 33 methods
// Provided methods
fn next(self) -> (Self::Item, Self::IntoIter)
where Self: Sized { ... }
fn peekable(self) -> Peekable<Self::IntoIter>
where Self: Sized { ... }
fn all<F>(self, f: F) -> bool
where Self: Sized,
F: FnMut(Self::Item) -> bool { ... }
fn any<F>(self, f: F) -> bool
where Self: Sized,
F: FnMut(Self::Item) -> bool { ... }
fn chain<U>(self, other: U) -> Chain<Self::IntoIter, U::IntoIter>
where Self: Sized,
U: IntoIterator<Item = Self::Item> { ... }
fn cloned<'a, T>(self) -> Cloned<Self>
where Self: Sized + NonEmptyIterator<Item = &'a T>,
T: Clone + 'a { ... }
fn collect<B>(self) -> B
where Self: Sized,
B: FromNonEmptyIterator<Self::Item> { ... }
fn copied<'a, T>(self) -> Copied<Self::IntoIter>
where Self: Sized + NonEmptyIterator<Item = &'a T>,
T: Copy + 'a { ... }
fn count(self) -> NonZeroUsize
where Self: Sized { ... }
fn enumerate(self) -> Enumerate<Self>
where Self: Sized { ... }
fn filter<P>(
self,
predicate: P,
) -> Filter<<Self as IntoIterator>::IntoIter, P>
where Self: Sized,
P: FnMut(&<Self as IntoIterator>::Item) -> bool { ... }
fn filter_map<B, F>(
self,
f: F,
) -> FilterMap<<Self as IntoIterator>::IntoIter, F>
where Self: Sized,
F: FnMut(<Self as IntoIterator>::Item) -> Option<B> { ... }
fn find<P>(self, predicate: P) -> Option<Self::Item>
where Self: Sized,
P: FnMut(&Self::Item) -> bool { ... }
fn flat_map<U, F>(self, f: F) -> FlatMap<Self::IntoIter, U, F>
where Self: Sized,
F: FnMut(Self::Item) -> U,
U: IntoNonEmptyIterator { ... }
fn fold<B, F>(self, init: B, f: F) -> B
where Self: Sized,
F: FnMut(B, Self::Item) -> B { ... }
fn group_by<K, F>(self, f: F) -> NEGroupBy<Self, F>
where Self: Sized,
F: FnMut(&Self::Item) -> K,
K: PartialEq { ... }
fn map<U, F>(self, f: F) -> Map<Self, F>
where Self: Sized,
F: FnMut(Self::Item) -> U { ... }
fn max(self) -> Self::Item
where Self: Sized,
Self::Item: Ord { ... }
fn max_by<F>(self, compare: F) -> Self::Item
where Self: Sized,
F: Fn(&Self::Item, &Self::Item) -> Ordering { ... }
fn max_by_key<B, F>(self, key: F) -> Self::Item
where Self: Sized,
B: Ord,
F: FnMut(&Self::Item) -> B { ... }
fn min(self) -> Self::Item
where Self: Sized,
Self::Item: Ord { ... }
fn min_by<F>(self, compare: F) -> Self::Item
where Self: Sized,
F: Fn(&Self::Item, &Self::Item) -> Ordering { ... }
fn min_by_key<B, F>(self, key: F) -> Self::Item
where Self: Sized,
B: Ord,
F: FnMut(&Self::Item) -> B { ... }
fn sorted_by_key<K, F>(self, f: F) -> IntoIter<Self::Item>
where Self: Sized,
K: Ord,
F: FnMut(&Self::Item) -> K { ... }
fn nth(self, n: usize) -> Option<Self::Item>
where Self: Sized { ... }
fn skip(self, n: usize) -> Skip<<Self as IntoIterator>::IntoIter>
where Self: Sized { ... }
fn skip_while<P>(
self,
pred: P,
) -> SkipWhile<<Self as IntoIterator>::IntoIter, P>
where Self: Sized,
P: FnMut(&<Self as IntoIterator>::Item) -> bool { ... }
fn sum<S>(self) -> S
where Self: Sized + IntoIterator,
S: Sum<<Self as IntoIterator>::Item> { ... }
fn take(self, n: NonZeroUsize) -> Take<Self>
where Self: Sized { ... }
fn take_while<P>(
self,
pred: P,
) -> TakeWhile<<Self as IntoIterator>::IntoIter, P>
where Self: Sized,
P: FnMut(&<Self as IntoIterator>::Item) -> bool { ... }
fn product<P>(self) -> P
where Self: Sized + IntoIterator,
P: Product<<Self as IntoIterator>::Item> { ... }
fn zip<U>(self, other: U) -> Zip<Self::IntoIter, U::IntoIter>
where Self: Sized,
U: IntoNonEmptyIterator { ... }
fn reduce<F>(self, f: F) -> Self::Item
where Self: Sized,
F: FnMut(Self::Item, Self::Item) -> Self::Item { ... }
}Expand description
An Iterator that is guaranteed to have at least one item.
By implementing NonEmptyIterator for a type the implementor is responsible
for ensuring that non-emptiness holds. Violating this invariant may lead to
panics and/or undefined behavior.
Provided Methods§
Sourcefn next(self) -> (Self::Item, Self::IntoIter)where
Self: Sized,
fn next(self) -> (Self::Item, Self::IntoIter)where
Self: Sized,
Advances this non-empty iterator, consuming its ownership. Yields the first item and a possibly empty iterator containing the rest of the elements.
Sourcefn peekable(self) -> Peekable<Self::IntoIter>where
Self: Sized,
fn peekable(self) -> Peekable<Self::IntoIter>where
Self: Sized,
Creates an iterator which can use the peek and peek_mut methods
to look at the next element of the iterator without consuming it. See
their documentation for more information.
Note that the underlying iterator is still advanced when this method is
called. In order to retrieve the next element, next is called on the
underlying iterator, hence any side effects (i.e. anything other than
fetching the next value) of the next method will occur.
§Examples
use nonempty_collections::*;
let v = nev![0, 1, 2, 3];
let iter = v.into_nonempty_iter().peekable();
assert_eq!(&0, iter.peek());Sourcefn all<F>(self, f: F) -> bool
fn all<F>(self, f: F) -> bool
Tests if every element of the iterator matches a predicate.
Because this function always advances the iterator at least once, the
non-empty guarantee is invalidated. Therefore, this function consumes
this NonEmptyIterator.
See also Iterator::all.
§Examples
use nonempty_collections::*;
let n = nev![2, 2, 2];
assert!(n.nonempty_iter().all(|n| n % 2 == 0));
assert!(n.iter().all(|n| n % 2 == 0));Sourcefn any<F>(self, f: F) -> bool
fn any<F>(self, f: F) -> bool
Tests if any element of the iterator matches a predicate.
Because this function always advances the iterator at least once, the
non-empty guarantee is invalidated. Therefore, this function consumes
this NonEmptyIterator.
See also Iterator::any.
§Examples
use nonempty_collections::*;
let n = nev![1, 1, 1, 2, 1, 1];
assert!(n.nonempty_iter().any(|n| n % 2 == 0));
assert!(!n.nonempty_iter().any(|n| n % 3 == 0));Sourcefn chain<U>(self, other: U) -> Chain<Self::IntoIter, U::IntoIter>
fn chain<U>(self, other: U) -> Chain<Self::IntoIter, U::IntoIter>
Takes two iterators and creates a new non-empty iterator over both in sequence.
Note that the second iterator need not be empty.
See also Iterator::chain.
use nonempty_collections::*;
let v = nev![1, 2, 3];
let s = nes![4, 5, 6];
let mut r: Vec<_> = v.into_nonempty_iter().chain(s).collect();
r.sort();
assert_eq!(vec![1, 2, 3, 4, 5, 6], r);Sourcefn cloned<'a, T>(self) -> Cloned<Self>
fn cloned<'a, T>(self) -> Cloned<Self>
Creates a non-empty iterator which clones all of its elements.
This is useful when you have an iterator over &T, but you need an
iterator over T.
See also Iterator::cloned.
use nonempty_collections::NEVec;
use nonempty_collections::*;
#[derive(Debug, Clone, PartialEq, Eq)]
enum Foo {
A,
B,
C,
}
let v0 = nev![Foo::A, Foo::B, Foo::C];
let v1: NEVec<_> = v0.nonempty_iter().collect();
let v2: NEVec<_> = v0.nonempty_iter().cloned().collect();
assert_eq!(nev![&Foo::A, &Foo::B, &Foo::C], v1);
assert_eq!(nev![Foo::A, Foo::B, Foo::C], v2);Sourcefn collect<B>(self) -> B
fn collect<B>(self) -> B
Transforms an iterator into a collection, or some other concrete value.
See also Iterator::collect.
use nonempty_collections::*;
let n0 = nev![1, 2, 3, 4];
let n1 = n0.into_nonempty_iter().collect();
assert_eq!(nev![1, 2, 3, 4], n1);Sourcefn copied<'a, T>(self) -> Copied<Self::IntoIter>
fn copied<'a, T>(self) -> Copied<Self::IntoIter>
Creates a non-empty iterator which copies all of its elements.
See also Iterator::copied.
use nonempty_collections::*;
let n0 = nev![1, 2, 3, 4];
let n1 = n0.nonempty_iter().copied().collect();
assert_eq!(n0, n1);Sourcefn count(self) -> NonZeroUsizewhere
Self: Sized,
fn count(self) -> NonZeroUsizewhere
Self: Sized,
Consumes the non-empty iterator, counting the number of iterations and returning it.
See also Iterator::count.
use nonempty_collections::*;
let n = nev![1];
assert_eq!(1, n.nonempty_iter().count().get());
let n = nev![1, 2, 3, 4, 5, 6];
assert_eq!(6, n.nonempty_iter().count().get());Sourcefn enumerate(self) -> Enumerate<Self>where
Self: Sized,
fn enumerate(self) -> Enumerate<Self>where
Self: Sized,
Creates a non-empty iterator which gives the current iteration count as well as the next value.
See also Iterator::enumerate.
use nonempty_collections::*;
let s = nes!["Doriath", "Gondolin", "Nargothrond"];
let total: usize = s.nonempty_iter().enumerate().map(|(c, _)| c).sum();
assert_eq!(3, total);Sourcefn filter<P>(self, predicate: P) -> Filter<<Self as IntoIterator>::IntoIter, P>
fn filter<P>(self, predicate: P) -> Filter<<Self as IntoIterator>::IntoIter, P>
Creates an iterator which uses a closure to determine if an element should be yielded.
Note: The iterator returned by this method is not a
NonEmptyIterator, since there is never a guarantee that any element
will pass the predicate.
See also Iterator::filter.
use nonempty_collections::*;
let n = nev![1, 2, 3, 4, 5, 6];
let v: Vec<_> = n
.nonempty_iter()
.map(|x| x * 2)
.filter(|&x| x % 3 == 0)
.collect();
assert_eq!(vec![6, 12], v);Sourcefn filter_map<B, F>(
self,
f: F,
) -> FilterMap<<Self as IntoIterator>::IntoIter, F>
fn filter_map<B, F>( self, f: F, ) -> FilterMap<<Self as IntoIterator>::IntoIter, F>
Creates an iterator that both filters and maps.
Note: The iterator returned by this method is not a
NonEmptyIterator, since there is never a guarantee that any element
will yield Some from the given function.
See also Iterator::filter_map.
use nonempty_collections::*;
let v = nev!["Frodo", "Sam", "", "Peregrin", "Meriadoc"];
let firsts: Vec<char> = v
.into_nonempty_iter()
.filter_map(|s| s.chars().next())
.collect();
assert_eq!(vec!['F', 'S', 'P', 'M'], firsts);Sourcefn find<P>(self, predicate: P) -> Option<Self::Item>
fn find<P>(self, predicate: P) -> Option<Self::Item>
Searches for an element of an iterator that satisfies a predicate.
Because this function always advances the iterator at least once, the
non-empty guarantee is invalidated. Therefore, this function consumes
this NonEmptyIterator.
See also Iterator::find.
§Examples
use nonempty_collections::*;
let n = nev![1, 3, 5, 7, 9, 10];
assert_eq!(Some(&10), n.iter().find(|n| *n % 2 == 0));
assert_eq!(None, n.iter().find(|n| **n > 10));Sourcefn flat_map<U, F>(self, f: F) -> FlatMap<Self::IntoIter, U, F>
fn flat_map<U, F>(self, f: F) -> FlatMap<Self::IntoIter, U, F>
Creates an iterator that works like map, but flattens nested,
non-empty structure.
See also Iterator::flat_map.
use nonempty_collections::*;
let v = nev![1, 2, 3];
let r = v.into_nonempty_iter().flat_map(|n| nev![n]).collect();
assert_eq!(nev![1, 2, 3], r);Sourcefn fold<B, F>(self, init: B, f: F) -> B
fn fold<B, F>(self, init: B, f: F) -> B
Folds every element into an accumulator by applying an operation, returning the final result.
See also Iterator::fold.
use nonempty_collections::*;
let n = nev![1, 2, 3, 4];
let r = n.into_nonempty_iter().fold(0, |acc, x| acc + x);
assert_eq!(10, r);Sourcefn group_by<K, F>(self, f: F) -> NEGroupBy<Self, F>
fn group_by<K, F>(self, f: F) -> NEGroupBy<Self, F>
Group the non-empty input stream into concrete, non-empty subsections
via a given function. The cutoff criterion is whether the return value
of f changes between two consecutive elements.
use nonempty_collections::*;
let n = nev![1, 1, 2, 3, 3];
let r: NEVec<_> = n.into_nonempty_iter().group_by(|n| *n).collect();
assert_eq!(r, nev![nev![1, 1], nev![2], nev![3, 3]]);
let n = nev![2, 4, 6, 7, 9, 1, 2, 4, 6, 3];
let r: NEVec<_> = n.into_nonempty_iter().group_by(|n| n % 2 == 0).collect();
assert_eq!(
r,
nev![nev![2, 4, 6], nev![7, 9, 1], nev![2, 4, 6], nev![3]]
);Sourcefn map<U, F>(self, f: F) -> Map<Self, F>
fn map<U, F>(self, f: F) -> Map<Self, F>
Takes a closure and creates a non-empty iterator which calls that closure on each element.
If self is a NonEmptyIterator, then so is Map.
See also Iterator::map.
use nonempty_collections::NEVec;
use nonempty_collections::*;
let s = nes![1, 2, 3];
let mut v: NEVec<_> = s.nonempty_iter().map(|n| n * 2).collect();
v.sort();
assert_eq!(nev![2, 4, 6], v);Sourcefn max(self) -> Self::Item
fn max(self) -> Self::Item
Returns the maximum element of a non-empty iterator.
Unlike Iterator::max, this always yields a value.
use nonempty_collections::*;
let v = nev![1, 1000, 2, 3];
assert_eq!(1000, v.into_nonempty_iter().max());Sourcefn max_by<F>(self, compare: F) -> Self::Item
fn max_by<F>(self, compare: F) -> Self::Item
Returns the element that gives the maximum value with respect to the given comparison function.
Unlike Iterator::max_by, this always yields a value.
Sourcefn max_by_key<B, F>(self, key: F) -> Self::Item
fn max_by_key<B, F>(self, key: F) -> Self::Item
Returns the element that gives the maximum value from the specified function.
There are two differences with Iterator::max_by_key:
- this function always yields a value while
Iterator::max_by_keyyields anOption. - if several elements are equally maximum, the first element is
returned (unlike
Iterator::max_by_keywhich returns the last element).
§Examples
use nonempty_collections::*;
let max = nev!["hi", "hey", "rust", "yolo"]
.into_nonempty_iter()
.max_by_key(|item| item.len());
assert_eq!("rust", max);Sourcefn min(self) -> Self::Item
fn min(self) -> Self::Item
Returns the minimum element of a non-empty iterator.
Unlike Iterator::min, this always yields a value.
use nonempty_collections::*;
let v = nev![1000, 1, 2000, 3000];
assert_eq!(1, v.into_nonempty_iter().min());Sourcefn min_by<F>(self, compare: F) -> Self::Item
fn min_by<F>(self, compare: F) -> Self::Item
Returns the element that gives the minimum value with respect to the given comparison function.
Unlike Iterator::min_by, this always yields a value.
Sourcefn min_by_key<B, F>(self, key: F) -> Self::Item
fn min_by_key<B, F>(self, key: F) -> Self::Item
Returns the element that gives the minimum value from the specified function.
There are two differences with Iterator::min_by_key:
- this function always yields a value while
Iterator::min_by_keyyields anOption. - if several elements are equally minimum, the first element is
returned (unlike
Iterator::min_by_keywhich returns the last element).
§Examples
use nonempty_collections::*;
let min = nev!["hi", "hello", "greetings", "hy"]
.into_nonempty_iter()
.min_by_key(|item| item.len());
assert_eq!("hi", min);Sourcefn sorted_by_key<K, F>(self, f: F) -> IntoIter<Self::Item>
fn sorted_by_key<K, F>(self, f: F) -> IntoIter<Self::Item>
Sort all iterator elements into a new non-empty iterator in ascending order.
Note: This consumes the entire iterator, uses the
NEVec::sort_by_key method and returns the result as a new iterator
that owns its elements.
This sort is stable (i.e., does not reorder equal elements).
The sorted iterator, if directly collected to a NEVec, is converted
without any extra copying or allocation cost.
use nonempty_collections::*;
// sort people in descending order by age
let people = nev![("Jane", 20), ("John", 18), ("Jill", 30), ("Jack", 30)];
let oldest_people_first = people
.into_nonempty_iter()
.sorted_by_key(|x| -x.1)
.map(|(person, _age)| person)
.collect::<NEVec<_>>();
assert_eq!(nev!["Jill", "Jack", "Jane", "John"], oldest_people_first);Sourcefn nth(self, n: usize) -> Option<Self::Item>where
Self: Sized,
fn nth(self, n: usize) -> Option<Self::Item>where
Self: Sized,
Returns the nth element of the iterator.
This function consumes this NonEmptyIterator. Self::next() can be
used for getting the first element and a reference to an iterator
over the remaining elements.
See also Iterator::nth.
use nonempty_collections::*;
let n = nev![0, 1, 2, 3, 4, 5, 6];
assert_eq!(Some(&0), n.nonempty_iter().nth(0));
let n = nev![0, 1, 2, 3, 4, 5, 6];
assert_eq!(Some(&6), n.nonempty_iter().nth(6));
let n = nev![0, 1, 2, 3, 4, 5, 6];
assert_eq!(None, n.nonempty_iter().nth(100));Sourcefn skip(self, n: usize) -> Skip<<Self as IntoIterator>::IntoIter>where
Self: Sized,
fn skip(self, n: usize) -> Skip<<Self as IntoIterator>::IntoIter>where
Self: Sized,
Skip the first n elements.
Note that the result will not be non-empty.
See also Iterator::skip.
use nonempty_collections::*;
let v = nev![1, 2, 3];
assert_eq!(Some(&3), v.nonempty_iter().skip(2).next());Sourcefn skip_while<P>(
self,
pred: P,
) -> SkipWhile<<Self as IntoIterator>::IntoIter, P>
fn skip_while<P>( self, pred: P, ) -> SkipWhile<<Self as IntoIterator>::IntoIter, P>
Skips over all initial elements that pass a given predicate.
Note: This does not yield a non-empty iterator, since there is no guarantee that anything will fail the predicate.
See also Iterator::skip_while.
use nonempty_collections::*;
let v = nev![2, 4, 6, 7, 8];
let r: Vec<_> = v.into_nonempty_iter().skip_while(|n| n % 2 == 0).collect();
assert_eq!(vec![7, 8], r);Sourcefn sum<S>(self) -> S
fn sum<S>(self) -> S
Sums the elements of a non-empty iterator.
See also Iterator::sum.
use nonempty_collections::*;
let sum: u32 = nev![1, 2, 3, 4].nonempty_iter().sum();
assert_eq!(10, sum);Sourcefn take(self, n: NonZeroUsize) -> Take<Self>where
Self: Sized,
fn take(self, n: NonZeroUsize) -> Take<Self>where
Self: Sized,
Iterates over the first n elements, or fewer if the underlying
iterator ends sooner.
See also Iterator::take.
§Panics
Panics if n == 0.
§Examples
use core::num::NonZeroUsize;
use nonempty_collections::*;
let n: NEVec<_> = nev![1, 2, 3]
.nonempty_iter()
.map(|n| n * 2)
.take(NonZeroUsize::new(2).unwrap())
.collect();
assert_eq!(nev![2, 4], n);Sourcefn take_while<P>(
self,
pred: P,
) -> TakeWhile<<Self as IntoIterator>::IntoIter, P>
fn take_while<P>( self, pred: P, ) -> TakeWhile<<Self as IntoIterator>::IntoIter, P>
Iterates over all initial elements that pass a given predicate.
Note: This does not yield a non-empty iterator, since there is no guarantee that anything will pass the predicate.
See also Iterator::take_while.
use nonempty_collections::*;
let v = nev![2, 4, 6, 7, 8];
let r: Vec<_> = v.into_nonempty_iter().take_while(|n| n % 2 == 0).collect();
assert_eq!(vec![2, 4, 6], r);Sourcefn product<P>(self) -> P
fn product<P>(self) -> P
Iterates over the entire non-empty iterator, multiplying all the elements.
See also Iterator::product.
use nonempty_collections::*;
let prod: u32 = nev![1, 2, 3, 4].nonempty_iter().product();
assert_eq!(24, prod);Sourcefn zip<U>(self, other: U) -> Zip<Self::IntoIter, U::IntoIter>where
Self: Sized,
U: IntoNonEmptyIterator,
fn zip<U>(self, other: U) -> Zip<Self::IntoIter, U::IntoIter>where
Self: Sized,
U: IntoNonEmptyIterator,
“Zips up” two non-empty iterators into a single one, while preserving non-emptiness.
See also Iterator::zip.
use nonempty_collections::*;
let a = nev![1, 2, 3];
let b = nev![4, 5, 6, 7];
let r = a
.into_nonempty_iter()
.zip(b)
.map(|(av, bv)| av + bv)
.collect();
assert_eq!(nev![5, 7, 9], r);Sourcefn reduce<F>(self, f: F) -> Self::Item
fn reduce<F>(self, f: F) -> Self::Item
Reduces the elements to a single one, by repeatedly applying a reducing operation.
See also Iterator::reduce.
use nonempty_collections::*;
let a = nev![1, 2, 3, 4];
let b = a.clone();
let x = a.into_nonempty_iter().reduce(|acc, v| acc + v);
assert_eq!(x, 10);
let y = b.into_nonempty_iter().reduce(|acc, v| acc * v);
assert_eq!(y, 24);