Trait Itertools
pub trait Itertools: Iterator
An Iterator blanket implementation that provides extra adaptors and
methods.
This trait defines a number of methods. They are divided into two groups:
-
Adaptors take an iterator and parameter as input, and return a new iterator value. These are listed first in the trait. An example of an adaptor is
.interleave() -
Regular methods are those that don't return iterators and instead return a regular value of some other kind.
.next_tuple()is an example and the first regular method in the list.
Provided Methods
fn interleave<J>(self, other: J) -> Interleave<Self, J::IntoIter> where J: IntoIterator<Item = Self::Item>, Self: Sized,Alternate elements from two iterators until both have run out.
Iterator element type is
Self::Item.This iterator is fused.
use Itertools; let it = .interleave; assert_equal;fn interleave_shortest<J>(self, other: J) -> InterleaveShortest<Self, J::IntoIter> where J: IntoIterator<Item = Self::Item>, Self: Sized,Alternate elements from two iterators until at least one of them has run out.
Iterator element type is
Self::Item.use Itertools; let it = .interleave_shortest; assert_equal;fn intersperse(self, element: Self::Item) -> Intersperse<Self> where Self: Sized, Self::Item: Clone,An iterator adaptor to insert a particular value between each element of the adapted iterator.
Iterator element type is
Self::Item.This iterator is fused.
use Itertools; assert_equal;fn intersperse_with<F>(self, element: F) -> IntersperseWith<Self, F> where Self: Sized, F: FnMut() -> Self::Item,An iterator adaptor to insert a particular value created by a function between each element of the adapted iterator.
Iterator element type is
Self::Item.This iterator is fused.
use Itertools; let mut i = 10; assert_equal; assert_eq!;fn get<R>(self, index: R) -> R::Output where Self: Sized, R: IteratorIndex<Self>,Returns an iterator over a subsection of the iterator.
Works similarly to
slice::get.Panics for ranges
..=usize::MAXand0..=usize::MAX.It's a generalisation of
Iterator::takeandIterator::skip, and uses these under the hood. Therefore, the resulting iterator is:ExactSizeIteratorif the adapted iterator isExactSizeIterator.DoubleEndedIteratorif the adapted iterator isDoubleEndedIteratorandExactSizeIterator.
Unspecified Behavior
The result of indexing with an exhausted
core::ops::RangeInclusiveis unspecified.Examples
use Itertools; let vec = vec!; let mut range: = vec.iter.get.copied.collect; assert_eq!; // It works with other types of ranges, too range = vec.iter.get.copied.collect; assert_eq!; range = vec.iter.get.copied.collect; assert_eq!; range = vec.iter.get.copied.collect; assert_eq!; range = vec.iter.get.copied.collect; assert_eq!; range = vec.iter.get.copied.collect; assert_eq!;fn zip_longest<J>(self, other: J) -> ZipLongest<Self, J::IntoIter> where J: IntoIterator, Self: Sized,Create an iterator which iterates over both this and the specified iterator simultaneously, yielding pairs of two optional elements.
This iterator is fused.
As long as neither input iterator is exhausted yet, it yields two values via
EitherOrBoth::Both.When the parameter iterator is exhausted, it only yields a value from the
selfiterator viaEitherOrBoth::Left.When the
selfiterator is exhausted, it only yields a value from the parameter iterator viaEitherOrBoth::Right.When both iterators return
None, all further invocations of.next()will returnNone.Iterator element type is
EitherOrBoth<Self::Item, J::Item>.use ; use Itertools; let it = .zip_longest; assert_equal;fn zip_eq<J>(self, other: J) -> ZipEq<Self, J::IntoIter> where J: IntoIterator, Self: Sized,Create an iterator which iterates over both this and the specified iterator simultaneously, yielding pairs of elements.
Panics if the iterators reach an end and they are not of equal lengths.
Examples
use Itertools; let a = vec!; let b = vec!; let zipped: = a.into_iter.zip_eq.collect; assert_equal;use itertools::Itertools; let a = [1, 2]; let b = [3, 4, 5]; // This example panics because the iterators are not of equal length. let _zipped: Vec<_> = a.iter().zip_eq(b.iter()).collect();fn batching<B, F>(self, f: F) -> Batching<Self, F> where F: FnMut(&mut Self) -> Option<B>, Self: Sized,A “meta iterator adaptor”. Its closure receives a reference to the iterator and may pick off as many elements as it likes, to produce the next iterator element.
Iterator element type is
B.use Itertools; // An adaptor that gathers elements in pairs let pit = .batching; assert_equal;fn chunk_by<K, F>(self, key: F) -> ChunkBy<K, Self, F> where Self: Sized, F: FnMut(&Self::Item) -> K, K: PartialEq,Return an iterable that can group iterator elements. Consecutive elements that map to the same key (“runs”), are assigned to the same group.
ChunkByis the storage for the lazy grouping operation.If the groups are consumed in order, or if each group's iterator is dropped without keeping it around, then
ChunkByuses no allocations. It needs allocations only if several group iterators are alive at the same time.This type implements
IntoIterator(it is not an iterator itself), because the group iterators need to borrow from this value. It should be stored in a local variable or temporary and iterated.Iterator element type is
(K, Group): the group's key and the group iterator.use Itertools; // chunk data into runs of larger than zero or not. let data = vec!; // chunks: |---->|------>|--------->| // Note: The `&` is significant here, `ChunkBy` is iterable // only by reference. You can also call `.into_iter()` explicitly. let mut data_grouped = Vecnew; for in &data.into_iter.chunk_by assert_eq!;fn group_by<K, F>(self, key: F) -> ChunkBy<K, Self, F> where Self: Sized, F: FnMut(&Self::Item) -> K, K: PartialEq,See
.chunk_by().fn chunks(self, size: usize) -> IntoChunks<Self> where Self: Sized,Return an iterable that can chunk the iterator.
Yield subiterators (chunks) that each yield a fixed number elements, determined by
size. The last chunk will be shorter if there aren't enough elements.IntoChunksis based onChunkBy: it is iterable (implementsIntoIterator, notIterator), and it only buffers if several chunk iterators are alive at the same time.Iterator element type is
Chunk, each chunk's iterator.Panics if
sizeis 0.Examples
use Itertools; let data = vec!; //chunk size=3 |------->|-------->|--->| // Note: The `&` is significant here, `IntoChunks` is iterable // only by reference. You can also call `.into_iter()` explicitly. for chunk in &data.into_iter.chunksuse itertools::Itertools; let data = vec![1, 2, 3]; // Panics because chunk size is 0. let _chunks = data.into_iter().chunks(0);fn tuple_windows<T>(self) -> TupleWindows<Self, T> where Self: Sized + Iterator<Item = T::Item>, T: HomogeneousTuple, T::Item: Clone,Return an iterator over all contiguous windows producing tuples of a specific size (up to 12).
tuple_windowsclones the iterator elements so that they can be part of successive windows, this makes it most suited for iterators of references and other values that are cheap to copy.use Itertools; let mut v = Vecnew; // pairwise iteration for in .tuple_windows assert_eq!; let mut it = .tuple_windows; assert_eq!; assert_eq!; assert_eq!; // this requires a type hint let it = .; assert_equal; // you can also specify the complete type use TupleWindows; use Range; let it: = .tuple_windows; assert_equal;fn circular_tuple_windows<T>(self) -> CircularTupleWindows<Self, T> where Self: Sized + Clone + Iterator<Item = T::Item> + ExactSizeIterator, T: TupleCollect + Clone, T::Item: Clone,Return an iterator over all windows, wrapping back to the first elements when the window would otherwise exceed the length of the iterator, producing tuples of a specific size (up to 12).
circular_tuple_windowsclones the iterator elements so that they can be part of successive windows, this makes it most suited for iterators of references and other values that are cheap to copy.use Itertools; let mut v = Vecnew; for in .circular_tuple_windows assert_eq!; let mut it = .circular_tuple_windows; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; // this requires a type hint let it = .; assert_equal;fn tuples<T>(self) -> Tuples<Self, T> where Self: Sized + Iterator<Item = T::Item>, T: HomogeneousTuple,Return an iterator that groups the items in tuples of a specific size (up to 12).
See also the method
.next_tuple().use Itertools; let mut v = Vecnew; for in .tuples assert_eq!; let mut it = .tuples; assert_eq!; assert_eq!; assert_eq!; // this requires a type hint let it = .; assert_equal; // you can also specify the complete type use Tuples; use Range; let it: = .tuples; assert_equal;See also
Tuples::into_buffer.fn array_windows<const N: usize>(self) -> ArrayWindows<Self, N> where Self: Sized, Self::Item: Clone,Return an iterator over all contiguous windows, producing arrays of size
N.array_windowsclones the iterator elements so that they can be part of successive windows. This makes it most suited for iterators of references and other values that are cheap to copy.If the input iterator contains fewer than
Nitems, no windows are returned. Otherwise, if the input iterator containskitems, exactlyk+N-1windows are returned.(This formula still applies when
N==0, and means thatk+1zero-length windows are returned forkinput items.)use Itertools; // Three-element windows from the items [1, 2, 3, 4, 5]. assert_equal; // When the input list is shorter than the window size, no windows // are returned at all. let mut windows = .; assert_eq!; // When the window size is zero, one more window is returned // than there are items. assert_equal; // In some cases you don't have to specify the window size // explicitly with a type hint, because Rust can infer it for in .array_windowsfn circular_array_windows<const N: usize>(self) -> CircularArrayWindows<Self, N> where Self: Sized, Self::Item: Clone,Return an iterator over all windows, wrapping back to the first elements when the window would otherwise exceed the length of the iterator, producing arrays of size
N.circular_array_windowsclones the iterator elements so that they can be part of successive windows, this makes it most suited for iterators of references and other values that are cheap to copy.One window is returned per element of the input iterator. This is true even if the input contains fewer elements than the window size. In that situation, input elements are repeated within each window. The results are as if the input had been treated as a cyclic list, and a window of
Nitems had been returned for every starting point in the cycle.(If the window size is zero, the function still returns one empty window per element of the input iterator.)
use Itertools; // Three-element windows from [1, 2, 3, 4, 5], with two of // them wrapping round from 5 to 1. assert_equal; // If the input is shorter than the window size, input // items are repeated even within the same window. assert_equal; // If the input contains only one item, the returned window // repeats it N times. let once = once; assert_equal; // If the input is empty, no windows are returned at all. let empty = ; let mut windows = empty.; assert_eq!; // If the input is empty, no windows are returned at all. let empty = ; let mut windows = empty.; assert_eq!; // One window is returned per item, even if the windows are empty. assert_equal; // In some cases you don't have to specify the window size // explicitly with a type hint, because Rust can infer it. for in .circular_array_windowsfn tee(self) -> (Tee<Self>, Tee<Self>) where Self: Sized, Self::Item: Clone,Split into an iterator pair that both yield all elements from the original iterator.
Note: If the iterator is cloneable, prefer using that instead of using this method. Cloning is likely to be more efficient.
Iterator element type is
Self::Item.use Itertools; let xs = vec!; let = xs.into_iter.tee; assert_equal; assert_equal; assert_equal;fn map_into<R>(self) -> MapInto<Self, R> where Self: Sized, Self::Item: Into<R>,Convert each item of the iterator using the
Intotrait.use Itertools; assert_eq!;fn map_ok<F, T, U, E>(self, f: F) -> MapOk<Self, F> where Self: Iterator<Item = Result<T, E>> + Sized, F: FnMut(T) -> U,Return an iterator adaptor that applies the provided closure to every
Result::Okvalue.Result::Errvalues are unchanged.use Itertools; let input = vec!; let it = input.into_iter.map_ok; assert_equal;fn filter_ok<F, T, E>(self, f: F) -> FilterOk<Self, F> where Self: Iterator<Item = Result<T, E>> + Sized, F: FnMut(&T) -> bool,Return an iterator adaptor that filters every
Result::Okvalue with the provided closure.Result::Errvalues are unchanged.use Itertools; let input = vec!; let it = input.into_iter.filter_ok; assert_equal;fn filter_map_ok<F, T, U, E>(self, f: F) -> FilterMapOk<Self, F> where Self: Iterator<Item = Result<T, E>> + Sized, F: FnMut(T) -> Option<U>,Return an iterator adaptor that filters and transforms every
Result::Okvalue with the provided closure.Result::Errvalues are unchanged.use Itertools; let input = vec!; let it = input.into_iter.filter_map_ok; assert_equal;fn flatten_ok<T, E>(self) -> FlattenOk<Self, T, E> where Self: Iterator<Item = Result<T, E>> + Sized, T: IntoIterator,Return an iterator adaptor that flattens every
Result::Okvalue into a series ofResult::Okvalues.Result::Errvalues are unchanged.This is useful when you have some common error type for your crate and need to propagate it upwards, but the
Result::Okcase needs to be flattened.use Itertools; let input = vec!; let it = input.iter.cloned.flatten_ok; assert_equal; // This can also be used to propagate errors when collecting. let output_result: = it.collect; assert_eq!;fn process_results<F, T, E, R>(self, processor: F) -> Result<R, E> where Self: Iterator<Item = Result<T, E>> + Sized, F: FnOnce(ProcessResults<'_, Self, E>) -> R,“Lift” a function of the values of the current iterator so as to process an iterator of
Resultvalues instead.processoris a closure that receives an adapted version of the iterator as the only argument — the adapted iterator produces elements of typeT, as long as the original iterator producesOkvalues.If the original iterable produces an error at any point, the adapted iterator ends and it will return the error itself.
Otherwise, the return value from the closure is returned wrapped inside
Ok.Example
use Itertools; type Item = ; let first_values: = vec!; let second_values: = vec!; // “Lift” the iterator .max() method to work on the Ok-values. let first_max = first_values.into_iter.process_results; let second_max = second_values.into_iter.process_results; assert_eq!; assert!;fn merge<J>(self, other: J) -> Merge<Self, J::IntoIter> where Self: Sized, Self::Item: PartialOrd, J: IntoIterator<Item = Self::Item>,Return an iterator adaptor that merges the two base iterators in ascending order. If both base iterators are sorted (ascending), the result is sorted.
Iterator element type is
Self::Item.use Itertools; let a = .step_by; let b = .step_by; let it = a.merge; assert_equal;fn merge_by<J, F>(self, other: J, is_first: F) -> MergeBy<Self, J::IntoIter, F> where Self: Sized, J: IntoIterator<Item = Self::Item>, F: FnMut(&Self::Item, &Self::Item) -> bool,Return an iterator adaptor that merges the two base iterators in order. This is much like
.merge()but allows for a custom ordering.This can be especially useful for sequences of tuples.
Iterator element type is
Self::Item.use Itertools; let a = .zip; let b = .zip; let it = a.merge_by; assert_equal;fn merge_join_by<J, F, T>(self, other: J, cmp_fn: F) -> MergeJoinBy<Self, J::IntoIter, F> where J: IntoIterator, F: FnMut(&Self::Item, &J::Item) -> T, Self: Sized,Create an iterator that merges items from both this and the specified iterator in ascending order.
The function can either return an
Orderingvariant or a boolean.If
cmp_fnreturnsOrdering, it chooses whether to pair elements based on theOrderingreturned by the specified compare function. At any point, inspecting the tip of the iteratorsIandJas itemsiof typeI::Itemandjof typeJ::Itemrespectively, the resulting iterator will:- Emit
EitherOrBoth::Left(i)wheni < j, and removeifrom its source iterator - Emit
EitherOrBoth::Right(j)wheni > j, and removejfrom its source iterator - Emit
EitherOrBoth::Both(i, j)wheni == j, and remove bothiandjfrom their respective source iterators
use ; use Itertools; let a = vec!.into_iter; let b = .step_by; assert_equal;If
cmp_fnreturnsbool, it chooses whether to pair elements based on the boolean returned by the specified function. At any point, inspecting the tip of the iteratorsIandJas itemsiof typeI::Itemandjof typeJ::Itemrespectively, the resulting iterator will:- Emit
Either::Left(i)whentrue, and removeifrom its source iterator - Emit
Either::Right(j)whenfalse, and removejfrom its source iterator
It is similar to the
Orderingcase if the first argument is considered "less" than the second argument.use ; use Itertools; let a = vec!.into_iter; let b = .step_by; assert_equal;- Emit
fn kmerge(self) -> KMerge<<Self::Item as IntoIterator>::IntoIter> where Self: Sized, Self::Item: IntoIterator, <Self::Item as IntoIterator>::Item: PartialOrd,Return an iterator adaptor that flattens an iterator of iterators by merging them in ascending order. Duplicates are preserved.
If all base iterators are sorted (ascending), the result is sorted.
Iterator element type is
Self::Item.use Itertools; let a = .step_by; // [0, 3] let b = .step_by; // [1, 3, 5 ] let c = .step_by; // [2, 5] let it = vec!.into_iter.kmerge; assert_equal;fn kmerge_by<F>(self, first: F) -> KMergeBy<<Self::Item as IntoIterator>::IntoIter, F> where Self: Sized, Self::Item: IntoIterator, F: FnMut(&<Self::Item as IntoIterator>::Item, &<Self::Item as IntoIterator>::Item) -> bool,Return an iterator adaptor that flattens an iterator of iterators by merging them according to the given closure.
The closure
firstis called with two elements a, b and should returntrueif a is ordered before b.If all base iterators are sorted according to
first, the result is sorted.Iterator element type is
Self::Item.use Itertools; let a = vec!; let b = vec!; let mut it = vec!.into_iter.kmerge_by; assert_eq!; assert_eq!;fn cartesian_product<J>(self, other: J) -> Product<Self, J::IntoIter> where Self: Sized, Self::Item: Clone, J: IntoIterator, J::IntoIter: Clone,Return an iterator adaptor that iterates over the cartesian product of the element sets of two iterators
selfandJ.Iterator element type is
(Self::Item, J::Item).use Itertools; let it = .cartesian_product; assert_equal;fn multi_cartesian_product(self) -> MultiProduct<<Self::Item as IntoIterator>::IntoIter> where Self: Sized, Self::Item: IntoIterator, <Self::Item as IntoIterator>::IntoIter: Clone, <Self::Item as IntoIterator>::Item: Clone,Return an iterator adaptor that iterates over the cartesian product of all subiterators returned by meta-iterator
self.All provided iterators must yield the same
Itemtype. To generate the product of iterators yielding multiple types, use theiproductmacro instead.The iterator element type is
Vec<T>, whereTis the iterator element of the subiterators.Note that the iterator is fused.
use Itertools; let mut multi_prod = .map .multi_cartesian_product; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!;If the adapted iterator is empty, the result is an iterator yielding a single empty vector. This is known as the nullary cartesian product.
use Itertools; let mut nullary_cartesian_product = .map.multi_cartesian_product; assert_eq!; assert_eq!;fn coalesce<F>(self, f: F) -> Coalesce<Self, F> where Self: Sized, F: FnMut(Self::Item, Self::Item) -> Result<Self::Item, (Self::Item, Self::Item)>,Return an iterator adaptor that uses the passed-in closure to optionally merge together consecutive elements.
The closure
fis passed two elements,previousandcurrentand may return either (1)Ok(combined)to merge the two values or (2)Err((previous', current'))to indicate they can't be merged. In (2), the valueprevious'is emitted by the iterator. Either (1)combinedor (2)current'becomes the previous value when coalesce continues with the next pair of elements to merge. The value that remains at the end is also emitted by the iterator.Iterator element type is
Self::Item.This iterator is fused.
use Itertools; // sum same-sign runs together let data = vec!; assert_equal;fn dedup(self) -> Dedup<Self> where Self: Sized, Self::Item: PartialEq,Remove duplicates from sections of consecutive identical elements. If the iterator is sorted, all elements will be unique.
Iterator element type is
Self::Item.This iterator is fused.
use Itertools; let data = vec!; assert_equal;fn dedup_by<Cmp>(self, cmp: Cmp) -> DedupBy<Self, Cmp> where Self: Sized, Cmp: FnMut(&Self::Item, &Self::Item) -> bool,Remove duplicates from sections of consecutive identical elements, determining equality using a comparison function. If the iterator is sorted, all elements will be unique.
Iterator element type is
Self::Item.This iterator is fused.
use Itertools; let data = vec!; assert_equal;fn dedup_with_count(self) -> DedupWithCount<Self> where Self: Sized,Remove duplicates from sections of consecutive identical elements, while keeping a count of how many repeated elements were present. If the iterator is sorted, all elements will be unique.
Iterator element type is
(usize, Self::Item).This iterator is fused.
use Itertools; let data = vec!; assert_equal;fn dedup_by_with_count<Cmp>(self, cmp: Cmp) -> DedupByWithCount<Self, Cmp> where Self: Sized, Cmp: FnMut(&Self::Item, &Self::Item) -> bool,Remove duplicates from sections of consecutive identical elements, while keeping a count of how many repeated elements were present. This will determine equality using a comparison function. If the iterator is sorted, all elements will be unique.
Iterator element type is
(usize, Self::Item).This iterator is fused.
use Itertools; let data = vec!; assert_equal;fn duplicates(self) -> Duplicates<Self> where Self: Sized, Self::Item: Eq + Hash,Return an iterator adaptor that produces elements that appear more than once during the iteration. Duplicates are detected using hash and equality.
The iterator is stable, returning the duplicate items in the order in which they occur in the adapted iterator. Each duplicate item is returned exactly once. If an item appears more than twice, the second item is the item retained and the rest are discarded.
use Itertools; let data = vec!; assert_equal;fn duplicates_with_hasher<S>(self, hash_builder: S) -> Duplicates<Self, S> where Self: Sized, Self::Item: Eq + Hash, S: BuildHasher,Return an iterator which yields the same elements as the one returned by .duplicates(), but uses the specified hash builder to hash the elements for comparison.
Warning:
hash_builderis normally randomly generated, and is designed to allow it's users to be resistant to attacks that cause many collisions and very poor performance. Setting it manually using this function can expose a DoS attack vector.use RandomState; use Itertools; let data = vec!; assert_equal;fn duplicates_by<V, F>(self, f: F) -> DuplicatesBy<Self, V, F> where Self: Sized, V: Eq + Hash, F: FnMut(&Self::Item) -> V,Return an iterator adaptor that produces elements that appear more than once during the iteration. Duplicates are detected using hash and equality.
Duplicates are detected by comparing the key they map to with the keying function
fby hash and equality. The keys are stored in a hash map in the iterator.The iterator is stable, returning the duplicate items in the order in which they occur in the adapted iterator. Each duplicate item is returned exactly once. If an item appears more than twice, the second item is the item retained and the rest are discarded.
use Itertools; let data = vec!; assert_equal;fn duplicates_by_with_hasher<V, F, S>(self, f: F, hash_builder: S) -> DuplicatesBy<Self, V, F, S> where Self: Sized, V: Eq + Hash, F: FnMut(&Self::Item) -> V, S: BuildHasher,Return an iterator which yields the same elements as the one returned by .duplicates_by(), but uses the specified hash builder to hash the keys for comparison.
Warning:
hash_builderis normally randomly generated, and is designed to allow it's users to be resistant to attacks that cause many collisions and very poor performance. Setting it manually using this function can expose a DoS attack vector.use RandomState; use Itertools; let data = vec!; assert_equal;fn unique(self) -> Unique<Self> where Self: Sized, Self::Item: Clone + Eq + Hash,Return an iterator adaptor that filters out elements that have already been produced once during the iteration. Duplicates are detected using hash and equality.
Clones of visited elements are stored in a hash set in the iterator.
The iterator is stable, returning the non-duplicate items in the order in which they occur in the adapted iterator. In a set of duplicate items, the first item encountered is the item retained.
use Itertools; let data = vec!; assert_equal;fn unique_with_hasher<S>(self, hash_builder: S) -> Unique<Self, S> where Self: Sized, Self::Item: Clone + Eq + Hash, S: BuildHasher,Return an iterator which yields the same elements as the one returned by .unique(), but uses the specified hash builder to hash the elements for comparison.
Warning:
hash_builderis normally randomly generated, and is designed to allow it's users to be resistant to attacks that cause many collisions and very poor performance. Setting it manually using this function can expose a DoS attack vector.use RandomState; use Itertools; let data = vec!; assert_equal;fn unique_by<V, F>(self, f: F) -> UniqueBy<Self, V, F> where Self: Sized, V: Eq + Hash, F: FnMut(&Self::Item) -> V,Return an iterator adaptor that filters out elements that have already been produced once during the iteration.
Duplicates are detected by comparing the key they map to with the keying function
fby hash and equality. The keys are stored in a hash set in the iterator.The iterator is stable, returning the non-duplicate items in the order in which they occur in the adapted iterator. In a set of duplicate items, the first item encountered is the item retained.
use Itertools; let data = vec!; assert_equal;fn unique_by_with_hasher<V, F, S>(self, f: F, hash_builder: S) -> UniqueBy<Self, V, F, S> where Self: Sized, V: Eq + Hash, F: FnMut(&Self::Item) -> V, S: BuildHasher,Return an iterator which yields the same elements as the one returned by .unique_by(), but uses the specified hash builder to hash the elements for comparison.
Warning:
hash_builderis normally randomly generated, and is designed to allow it's users to be resistant to attacks that cause many collisions and very poor performance. Setting it manually using this function can expose a DoS attack vector.use RandomState; use Itertools; let data = vec!; assert_equal;fn peeking_take_while<F>(&mut self, accept: F) -> PeekingTakeWhile<'_, Self, F> where Self: Sized + PeekingNext, F: FnMut(&Self::Item) -> bool,Return an iterator adaptor that borrows from this iterator and takes items while the closure
acceptreturnstrue.This adaptor can only be used on iterators that implement
PeekingNextlike.peekable(),put_backand a few other collection iterators.The last and rejected element (first
false) is still available whenpeeking_take_whileis done.See also
.take_while_ref()which is a similar adaptor.fn take_while_ref<F>(&mut self, accept: F) -> TakeWhileRef<'_, Self, F> where Self: Clone, F: FnMut(&Self::Item) -> bool,Return an iterator adaptor that borrows from a
Clone-able iterator to only pick off elements while the predicateacceptreturnstrue.It uses the
Clonetrait to restore the original iterator so that the last and rejected element (firstfalse) is still available whentake_while_refis done.use Itertools; let mut hexadecimals = "0123456789abcdef".chars; let decimals = hexadecimals.take_while_ref .; assert_eq!; assert_eq!;fn take_while_inclusive<F>(self, accept: F) -> TakeWhileInclusive<Self, F> where Self: Sized, F: FnMut(&Self::Item) -> bool,Returns an iterator adaptor that consumes elements while the given predicate is
true, including the element for which the predicate first returnedfalse.The [
.take_while()][std::iter::Iterator::take_while] adaptor is useful when you want items satisfying a predicate, but to know when to stop taking elements, we have to consume that first element that doesn't satisfy the predicate. This adaptor includes that element where [.take_while()][std::iter::Iterator::take_while] would drop it.The [
.take_while_ref()][crate::Itertools::take_while_ref] adaptor serves a similar purpose, but this adaptor doesn't requireCloneing the underlying elements.# use Itertools; let items = vec!; let filtered: = items .into_iter .take_while_inclusive .collect; assert_eq!;# use Itertools; let items = vec!; let take_while_inclusive_result: = items .iter .copied .take_while_inclusive .collect; let take_while_result: = items .into_iter .take_while .collect; assert_eq!; assert_eq!; // both iterators have the same items remaining at this point---the 3 // is lost from the `take_while` vec# use Itertools; ; let non_cloneable_items: = vec! .into_iter .map .collect; let filtered: = non_cloneable_items .into_iter .take_while_inclusive .collect; let expected: = vec!.into_iter.map.collect; assert_eq!;fn while_some<A>(self) -> WhileSome<Self> where Self: Sized + Iterator<Item = Option<A>>,Return an iterator adaptor that filters
Option<A>iterator elements and producesA. Stops on the firstNoneencountered.Iterator element type is
A, the unwrapped element.use Itertools; // List all hexadecimal digits assert_equal;fn tuple_combinations<T>(self) -> TupleCombinations<Self, T> where Self: Sized, Self::Item: Clone, T: HasCombination<Self>,Return an iterator adaptor that iterates over the combinations of the elements from an iterator.
Iterator element can be any homogeneous tuple of type
Self::Itemwith size up to 12.Guarantees
If the adapted iterator is deterministic, this iterator adapter yields items in a reliable order.
use Itertools; let mut v = Vecnew; for in .tuple_combinations assert_eq!; let mut it = .tuple_combinations; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; // this requires a type hint let it = .; assert_equal; // you can also specify the complete type use TupleCombinations; use Range; let it: = .tuple_combinations; assert_equal;fn array_combinations<const K: usize>(self) -> ArrayCombinations<Self, K> where Self: Sized, Self::Item: Clone,Return an iterator adaptor that iterates over the combinations of the elements from an iterator.
Iterator element type is [Self::Item; K]. The iterator produces a new array per iteration, and clones the iterator elements.
Guarantees
If the adapted iterator is deterministic, this iterator adapter yields items in a reliable order.
use Itertools; let mut v = Vecnew; for in .array_combinations assert_eq!; let mut it = .array_combinations; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; // this requires a type hint let it = .; assert_equal; // you can also specify the complete type use ArrayCombinations; use Range; let it: = .array_combinations; assert_equal;fn combinations(self, k: usize) -> Combinations<Self> where Self: Sized, Self::Item: Clone,Return an iterator adaptor that iterates over the
k-length combinations of the elements from an iterator.Iterator element type is
Vec<Self::Item>. The iterator produces a newVecper iteration, and clones the iterator elements.Guarantees
If the adapted iterator is deterministic, this iterator adapter yields items in a reliable order.
use Itertools; let it = .combinations; assert_equal;Note: Combinations does not take into account the equality of the iterated values.
use Itertools; let it = vec!.into_iter.combinations; assert_equal;fn combinations_with_replacement(self, k: usize) -> CombinationsWithReplacement<Self> where Self: Sized, Self::Item: Clone,Return an iterator that iterates over the
k-length combinations of the elements from an iterator, with replacement.Iterator element type is
Vec<Self::Item>. The iterator produces a newVecper iteration, and clones the iterator elements.use Itertools; let it = .combinations_with_replacement; assert_equal;fn array_combinations_with_replacement<const K: usize>(self) -> CombinationsWithReplacementGeneric<Self, [usize; K]> where Self: Sized, Self::Item: Clone,Return an iterator that iterates over the
k-length combinations of the elements from an iterator, with replacement.Iterator element type is [Self::Item; K]. The iterator produces a new array per iteration, and clones the iterator elements.
use Itertools; let it = .; assert_equal;fn permutations(self, k: usize) -> Permutations<Self> where Self: Sized, Self::Item: Clone,Return an iterator adaptor that iterates over all k-permutations of the elements from an iterator.
Iterator element type is
Vec<Self::Item>with lengthk. The iterator produces a newVecper iteration, and clones the iterator elements.If
kis greater than the length of the input iterator, the resultant iterator adaptor will be empty.If you are looking for permutations with replacements, use
repeat_n(iter, k).multi_cartesian_product()instead.use Itertools; let perms = .permutations; assert_equal;Note: Permutations does not take into account the equality of the iterated values.
use Itertools; let it = vec!.into_iter.permutations; assert_equal;Note: The source iterator is collected lazily, and will not be re-iterated if the permutations adaptor is completed and re-iterated.
fn powerset(self) -> Powerset<Self> where Self: Sized, Self::Item: Clone,Return an iterator that iterates through the powerset of the elements from an iterator.
Iterator element type is
Vec<Self::Item>. The iterator produces a newVecper iteration, and clones the iterator elements.The powerset of a set contains all subsets including the empty set and the full input set. A powerset has length 2^n where n is the length of the input set.
Each
Vecproduced by this iterator represents a subset of the elements produced by the source iterator.use Itertools; let sets = .powerset.; assert_equal;fn pad_using<F>(self, min: usize, f: F) -> PadUsing<Self, F> where Self: Sized, F: FnMut(usize) -> Self::Item,Return an iterator adaptor that pads the sequence to a minimum length of
minby filling missing elements using a closuref.Iterator element type is
Self::Item.use Itertools; let it = .pad_using; assert_equal; let it = .pad_using; assert_equal; let it = .pad_using.rev; assert_equal;fn with_position(self) -> WithPosition<Self> where Self: Sized,Return an iterator adaptor that combines each element with a
Positionto ease special-case handling of the first or last elements.Iterator element type is
(Position, Self::Item)use ; let it = .with_position; assert_equal; let it = .with_position; assert_equal;fn positions<P>(self, predicate: P) -> Positions<Self, P> where Self: Sized, P: FnMut(Self::Item) -> bool,Return an iterator adaptor that yields the indices of all elements satisfying a predicate, counted from the start of the iterator.
Equivalent to
iter.enumerate().filter(|(_, v)| predicate(*v)).map(|(i, _)| i).use Itertools; let data = vec!; assert_equal; assert_equal;fn update<F>(self, updater: F) -> Update<Self, F> where Self: Sized, F: FnMut(&mut Self::Item),Return an iterator adaptor that applies a mutating function to each element before yielding it.
use Itertools; let input = vec!; let it = input.into_iter.update; assert_equal;fn next_array<const N: usize>(&mut self) -> Option<[Self::Item; N]> where Self: Sized,Advances the iterator and returns the next items grouped in an array of a specific size.
If there are enough elements to be grouped in an array, then the array is returned inside
Some, otherwiseNoneis returned.use Itertools; let mut iter = 1..5; assert_eq!;fn collect_array<const N: usize>(self) -> Option<[Self::Item; N]> where Self: Sized,Collects all items from the iterator into an array of a specific size.
If the number of elements inside the iterator is exactly equal to the array size, then the array is returned inside
Some, otherwiseNoneis returned.use Itertools; let iter = 1..3; if let Some = iter.collect_array elsefn next_tuple<T>(&mut self) -> Option<T> where Self: Sized + Iterator<Item = T::Item>, T: HomogeneousTuple,Advances the iterator and returns the next items grouped in a tuple of a specific size (up to 12).
If there are enough elements to be grouped in a tuple, then the tuple is returned inside
Some, otherwiseNoneis returned.use Itertools; let mut iter = 1..5; assert_eq!;fn collect_tuple<T>(self) -> Option<T> where Self: Sized + Iterator<Item = T::Item>, T: HomogeneousTuple,Collects all items from the iterator into a tuple of a specific size (up to 12).
If the number of elements inside the iterator is exactly equal to the tuple size, then the tuple is returned inside
Some, otherwiseNoneis returned.use Itertools; let iter = 1..3; if let Some = iter.collect_tuple elsefn find_position<P>(&mut self, pred: P) -> Option<(usize, Self::Item)> where P: FnMut(&Self::Item) -> bool,Find the position and value of the first element satisfying a predicate.
The iterator is not advanced past the first element found.
use Itertools; let text = "Hα"; assert_eq!;fn find_or_last<P>(self, predicate: P) -> Option<Self::Item> where Self: Sized, P: FnMut(&Self::Item) -> bool,Find the value of the first element satisfying a predicate or return the last element, if any.
The iterator is not advanced past the first element found.
use Itertools; let numbers = ; assert_eq!; assert_eq!; assert_eq!; // An iterator of Results can return the first Ok or the last Err: let input = vec!; assert_eq!; let input: = vec!; assert_eq!; assert_eq!;fn find_or_first<P>(self, predicate: P) -> Option<Self::Item> where Self: Sized, P: FnMut(&Self::Item) -> bool,Find the value of the first element satisfying a predicate or return the first element, if any.
The iterator is not advanced past the first element found.
use Itertools; let numbers = ; assert_eq!; assert_eq!; assert_eq!; // An iterator of Results can return the first Ok or the first Err: let input = vec!; assert_eq!; let input: = vec!; assert_eq!; assert_eq!;fn contains<Q>(&mut self, query: &Q) -> bool where Self: Sized, Self::Item: Borrow<Q>, Q: PartialEq + ?Sized,Returns
trueif the given item is present in this iterator.This method is short-circuiting. If the given item is present in this iterator, this method will consume the iterator up-to-and-including the item. If the given item is not present in this iterator, the iterator will be exhausted.
use Itertools; let mut iter = vec!.into_iter; // search `iter` for `B` assert_eq!; // `B` was found, so the iterator now rests at the item after `B` (i.e, `C`). assert_eq!; // search `iter` for `E` assert_eq!; // `E` wasn't found, so `iter` is now exhausted assert_eq!;fn all_equal(&mut self) -> bool where Self: Sized, Self::Item: PartialEq,Check whether all elements compare equal.
Empty iterators are considered to have equal elements:
use Itertools; let data = vec!; assert!; assert!; assert!; assert!; let data: = None; assert!;fn all_equal_value(&mut self) -> Result<Self::Item, AllEqualValueError<Self::Item>> where Self: Sized, Self::Item: PartialEq,If there are elements and they are all equal, return a single copy of that element. If there are no elements, return an Error containing None. If there are elements and they are not all equal, return a tuple containing the first two non-equal elements found.
use ; let data = vec!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; let data: = None; assert_eq!;fn all_unique(&mut self) -> bool where Self: Sized, Self::Item: Eq + Hash,Check whether all elements are unique (non equal).
Empty iterators are considered to have unique elements:
use Itertools; let data = vec!; assert!; assert!; assert!; let data: = None; assert!;fn all_unique_with_hasher<S>(&mut self, hash_builder: S) -> bool where Self: Sized, Self::Item: Eq + Hash, S: BuildHasher,Check whether all elements are unique (non equal). The specified hash builder is used for hashing the elements. See .all_unique.
use RandomState; use Itertools; let data = vec!; assert!; assert!; assert!; let data : = None; assert!;fn dropping(self, n: usize) -> Self where Self: Sized,Consume the first
nelements from the iterator eagerly, and return the same iterator again.It works similarly to
.skip(n)except it is eager and preserves the iterator type.use Itertools; let iter = "αβγ".chars.dropping; assert_equal;Fusing notes: if the iterator is exhausted by dropping, the result of calling
.next()again depends on the iterator implementation.fn dropping_back(self, n: usize) -> Self where Self: Sized + DoubleEndedIterator,Consume the last
nelements from the iterator eagerly, and return the same iterator again.This is only possible on double ended iterators.
nmay be larger than the number of elements.Note: This method is eager, dropping the back elements immediately and preserves the iterator type.
use Itertools; let init = vec!.into_iter.dropping_back; assert_equal;fn concat(self) -> Self::Item where Self: Sized, Self::Item: Extend<<<Self as Iterator>::Item as IntoIterator>::Item> + IntoIterator + Default,Combine all an iterator's elements into one element by using
Extend.This combinator will extend the first item with each of the rest of the items of the iterator. If the iterator is empty, the default value of
I::Itemis returned.use Itertools; let input = vec!; assert_eq!;fn collect_vec(self) -> Vec<Self::Item> where Self: Sized,.collect_vec()is simply a type specialization ofIterator::collect, for convenience.fn try_collect<T, U, E>(self) -> Result<U, E> where Self: Sized + Iterator<Item = Result<T, E>>, Result<U, E>: FromIterator<Result<T, E>>,.try_collect()is more convenient way of writing.collect::<Result<_, _>>()Example
use Itertools; use ; # let _ = do_stuff;fn set_from<'a, A: 'a, J>(&mut self, from: J) -> usize where Self: Iterator<Item = &'a mut A>, J: IntoIterator<Item = A>,Assign to each reference in
selffrom thefromiterator, stopping at the shortest of the two iterators.The
fromiterator is queried for its next element before theselfiterator, and if either is exhausted the method is done.Return the number of elements written.
use Itertools; let mut xs = ; xs.iter_mut.set_from; assert_eq!;fn join(&mut self, sep: &str) -> String where Self::Item: Display,Combine all iterator elements into one
String, separated bysep.Use the
Displayimplementation of each element.use Itertools; assert_eq!; assert_eq!;fn format(self, sep: &str) -> Format<'_, Self> where Self: Sized,Format all iterator elements, separated by
sep.All elements are formatted (any formatting trait) with
sepinserted between each element.Panics if the formatter helper is formatted more than once.
use Itertools; let data = ; assert_eq!;fn format_with<F>(self, sep: &str, format: F) -> FormatWith<'_, Self, F> where Self: Sized, F: FnMut(Self::Item, &mut dyn FnMut(&dyn Display) -> Result) -> Result,Format all iterator elements, separated by
sep.This is a customizable version of
.format().The supplied closure
formatis called once per iterator element, with two arguments: the element and a callback that takes a&Displayvalue, i.e. any reference to type that implementsDisplay.Using
&format_args!(...)is the most versatile way to apply custom element formatting. The callback can be called multiple times if needed.Panics if the formatter helper is formatted more than once.
use Itertools; let data = ; let data_formatter = data.iter.format_with; assert_eq!; // .format_with() is recursively composable let matrix = ; let matrix_formatter = matrix.iter.format_with; assert_eq!;fn fold_ok<A, E, B, F>(&mut self, start: B, f: F) -> Result<B, E> where Self: Iterator<Item = Result<A, E>>, F: FnMut(B, A) -> B,Fold
Resultvalues from an iterator.Only
Okvalues are folded. If no error is encountered, the folded value is returned insideOk. Otherwise, the operation terminates and returns the firstErrvalue it encounters. No iterator elements are consumed after the first error.The first accumulator value is the
startparameter. Each iteration passes the accumulator value and the next value insideOkto the fold functionfand its return value becomes the new accumulator value.For example the sequence Ok(1), Ok(2), Ok(3) will result in a computation like this:
# let start = 0; # let f = |x, y| x + y; let mut accum = start; accum = f(accum, 1); accum = f(accum, 2); accum = f(accum, 3); # let _ = accum;With a
startvalue of 0 and an addition as folding function, this effectively results in ((0 + 1) + 2) + 3use Itertools; use Add; let values = ; assert_eq!; assert!;fn fold_options<A, B, F>(&mut self, start: B, f: F) -> Option<B> where Self: Iterator<Item = Option<A>>, F: FnMut(B, A) -> B,Fold
Optionvalues from an iterator.Only
Somevalues are folded. If noNoneis encountered, the folded value is returned insideSome. Otherwise, the operation terminates and returnsNone. No iterator elements are consumed after theNone.This is the
Optionequivalent tofold_ok.use Itertools; use Add; let mut values = vec!.into_iter; assert_eq!; let mut more_values = vec!.into_iter; assert!; assert_eq!;fn fold1<F>(self, f: F) -> Option<Self::Item> where F: FnMut(Self::Item, Self::Item) -> Self::Item, Self: Sized,Accumulator of the elements in the iterator.
Like
.fold(), without a base case. If the iterator is empty, returnNone. With just one element, return it. Otherwise elements are accumulated in sequence using the closuref.use Itertools; assert_eq!; assert_eq!;fn tree_reduce<F>(self, f: F) -> Option<Self::Item> where F: FnMut(Self::Item, Self::Item) -> Self::Item, Self: Sized,Accumulate the elements in the iterator in a tree-like manner.
You can think of it as, while there's more than one item, repeatedly combining adjacent items. It does so in bottom-up-merge-sort order, however, so that it needs only logarithmic stack space.
This produces a call tree like the following (where the calls under an item are done after reading that item):
1 2 3 4 5 6 7 │ │ │ │ │ │ │ └─f └─f └─f │ │ │ │ │ └───f └─f │ │ └─────fWhich, for non-associative functions, will typically produce a different result than the linear call tree used by [
Iterator::reduce]:1 2 3 4 5 6 7 │ │ │ │ │ │ │ └─f─f─f─f─f─fIf
fis associative you should also decide carefully:For an iterator producing
nelements, bothIterator::reduceandtree_reducewill callfn - 1times. However,tree_reducewill callfon earlier intermediate results, which is beneficial forfthat allocate and produce longer results for longer arguments. For example iffcombines arguments usingformat!, thentree_reducewill operate on average on shorter arguments resulting in less bytes being allocated overall.Moreover, the output of
tree_reduceis preferable to that ofIterator::reducein certain cases. For example, building a binary search tree usingtree_reducewill result in a balanced tree with heightO(ln(n)), whileIterator::reducewill output a tree with heightO(n), essentially a linked list.If
fdoes not benefit from such a reordering, likeu32::wrapping_add, prefer the normalIterator::reduceinstead since it will most likely result in the generation of simpler code because the compiler is able to optimize it.use Itertools; let f = ; // The same tree as above assert_eq!; // Like reduce, an empty iterator produces None assert_eq!; // tree_reduce matches reduce for associative operations... assert_eq!; // ...but not for non-associative ones assert_ne!; let mut total_len_reduce = 0; let reduce_res = .map .reduce .unwrap; let mut total_len_tree_reduce = 0; let tree_reduce_res = .map .tree_reduce .unwrap; assert_eq!; assert_eq!; assert_eq!;fn tree_fold1<F>(self, f: F) -> Option<Self::Item> where F: FnMut(Self::Item, Self::Item) -> Self::Item, Self: Sized,See
.tree_reduce().fn fold_while<B, F>(&mut self, init: B, f: F) -> FoldWhile<B> where Self: Sized, F: FnMut(B, Self::Item) -> FoldWhile<B>,An iterator method that applies a function, producing a single, final value.
fold_while()is basically equivalent toIterator::foldbut with additional support for early exit via short-circuiting.use ; use Itertools; let numbers = ; let mut result = 0; // for loop: for i in &numbers // fold: let result2 = numbers.iter.fold; // fold_while: let result3 = numbers.iter.fold_while.into_inner; // they're the same assert_eq!; assert_eq!;The big difference between the computations of
result2andresult3is that whilefold()called the provided closure for every item of the callee iterator,fold_while()actually stopped iterating as soon as it encounteredFold::Done(_).fn sum1<S>(self) -> Option<S> where Self: Sized, S: Sum<Self::Item>,Iterate over the entire iterator and add all the elements.
An empty iterator returns
None, otherwiseSome(sum).Panics
When calling
sum1()and a primitive integer type is being returned, this method will panic if the computation overflows and debug assertions are enabled.Examples
use Itertools; let empty_sum = .; assert_eq!; let nonempty_sum = .; assert_eq!;fn product1<P>(self) -> Option<P> where Self: Sized, P: Product<Self::Item>,Iterate over the entire iterator and multiply all the elements.
An empty iterator returns
None, otherwiseSome(product).Panics
When calling
product1()and a primitive integer type is being returned, method will panic if the computation overflows and debug assertions are enabled.Examples
use Itertools; let empty_product = .; assert_eq!; let nonempty_product = .; assert_eq!;fn sorted_unstable(self) -> IntoIter<Self::Item> where Self: Sized, Self::Item: Ord,Sort all iterator elements into a new iterator in ascending order.
Note: This consumes the entire iterator, uses the
slice::sort_unstablemethod and returns the result as a new iterator that owns its elements.This sort is unstable (i.e., may reorder equal elements).
The sorted iterator, if directly collected to a
Vec, is converted without any extra copying or allocation cost.use Itertools; // sort the letters of the text in ascending order let text = "bdacfe"; assert_equal;fn sorted_unstable_by<F>(self, cmp: F) -> IntoIter<Self::Item> where Self: Sized, F: FnMut(&Self::Item, &Self::Item) -> Ordering,Sort all iterator elements into a new iterator in ascending order.
Note: This consumes the entire iterator, uses the
slice::sort_unstable_bymethod and returns the result as a new iterator that owns its elements.This sort is unstable (i.e., may reorder equal elements).
The sorted iterator, if directly collected to a
Vec, is converted without any extra copying or allocation cost.use Itertools; // sort people in descending order by age let people = vec!; let oldest_people_first = people .into_iter .sorted_unstable_by .map; assert_equal;fn sorted_unstable_by_key<K, F>(self, f: F) -> IntoIter<Self::Item> where Self: Sized, K: Ord, F: FnMut(&Self::Item) -> K,Sort all iterator elements into a new iterator in ascending order.
Note: This consumes the entire iterator, uses the
slice::sort_unstable_by_keymethod and returns the result as a new iterator that owns its elements.This sort is unstable (i.e., may reorder equal elements).
The sorted iterator, if directly collected to a
Vec, is converted without any extra copying or allocation cost.use Itertools; // sort people in descending order by age let people = vec!; let oldest_people_first = people .into_iter .sorted_unstable_by_key .map; assert_equal;fn sorted(self) -> IntoIter<Self::Item> where Self: Sized, Self::Item: Ord,Sort all iterator elements into a new iterator in ascending order.
Note: This consumes the entire iterator, uses the
slice::sortmethod 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
Vec, is converted without any extra copying or allocation cost.use Itertools; // sort the letters of the text in ascending order let text = "bdacfe"; assert_equal;fn sorted_by<F>(self, cmp: F) -> IntoIter<Self::Item> where Self: Sized, F: FnMut(&Self::Item, &Self::Item) -> Ordering,Sort all iterator elements into a new iterator in ascending order.
Note: This consumes the entire iterator, uses the
slice::sort_bymethod 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
Vec, is converted without any extra copying or allocation cost.use Itertools; // sort people in descending order by age let people = vec!; let oldest_people_first = people .into_iter .sorted_by .map; assert_equal;fn sorted_by_key<K, F>(self, f: F) -> IntoIter<Self::Item> where Self: Sized, K: Ord, F: FnMut(&Self::Item) -> K,Sort all iterator elements into a new iterator in ascending order.
Note: This consumes the entire iterator, uses the
slice::sort_by_keymethod 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
Vec, is converted without any extra copying or allocation cost.use Itertools; // sort people in descending order by age let people = vec!; let oldest_people_first = people .into_iter .sorted_by_key .map; assert_equal;fn sorted_by_cached_key<K, F>(self, f: F) -> IntoIter<Self::Item> where Self: Sized, K: Ord, F: FnMut(&Self::Item) -> K,Sort all iterator elements into a new iterator in ascending order. The key function is called exactly once per key.
Note: This consumes the entire iterator, uses the
slice::sort_by_cached_keymethod 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
Vec, is converted without any extra copying or allocation cost.use Itertools; // sort people in descending order by age let people = vec!; let oldest_people_first = people .into_iter .sorted_by_cached_key .map; assert_equal;fn k_smallest(self, k: usize) -> IntoIter<Self::Item> where Self: Sized, Self::Item: Ord,Sort the k smallest elements into a new iterator, in ascending order.
Note: This consumes the entire iterator, and returns the result as a new iterator that owns its elements. If the input contains less than k elements, the result is equivalent to
self.sorted().This is guaranteed to use
k * sizeof(Self::Item) + O(1)memory andO(n log k)time, withnthe number of elements in the input.The sorted iterator, if directly collected to a
Vec, is converted without any extra copying or allocation cost.Note: This is functionally-equivalent to
self.sorted().take(k)but much more efficient.use Itertools; // A random permutation of 0..15 let numbers = vec!; let five_smallest = numbers .into_iter .k_smallest; assert_equal;fn k_smallest_by<F>(self, k: usize, cmp: F) -> IntoIter<Self::Item> where Self: Sized, F: FnMut(&Self::Item, &Self::Item) -> Ordering,Sort the k smallest elements into a new iterator using the provided comparison.
The sorted iterator, if directly collected to a
Vec, is converted without any extra copying or allocation cost.This corresponds to
self.sorted_by(cmp).take(k)in the same way thatk_smallestcorresponds toself.sorted().take(k), in both semantics and complexity.Particularly, a custom heap implementation ensures the comparison is not cloned.
use Itertools; // A random permutation of 0..15 let numbers = vec!; let five_smallest = numbers .into_iter .k_smallest_by; assert_equal;fn k_smallest_by_key<F, K>(self, k: usize, key: F) -> IntoIter<Self::Item> where Self: Sized, F: FnMut(&Self::Item) -> K, K: Ord,Return the elements producing the k smallest outputs of the provided function.
The sorted iterator, if directly collected to a
Vec, is converted without any extra copying or allocation cost.This corresponds to
self.sorted_by_key(key).take(k)in the same way thatk_smallestcorresponds toself.sorted().take(k), in both semantics and complexity.Particularly, a custom heap implementation ensures the comparison is not cloned.
use Itertools; // A random permutation of 0..15 let numbers = vec!; let five_smallest = numbers .into_iter .k_smallest_by_key; assert_equal;fn k_smallest_relaxed(self, k: usize) -> IntoIter<Self::Item> where Self: Sized, Self::Item: Ord,Sort the k smallest elements into a new iterator, in ascending order, relaxing the amount of memory required.
Note: This consumes the entire iterator, and returns the result as a new iterator that owns its elements. If the input contains less than k elements, the result is equivalent to
self.sorted().This is guaranteed to use
2 * k * sizeof(Self::Item) + O(1)memory andO(n + k log k)time, withnthe number of elements in the input, meaning it uses more memory than the minimum obtained byk_smallestbut achieves linear time in the number of elements.The sorted iterator, if directly collected to a
Vec, is converted without any extra copying or allocation cost.Note: This is functionally-equivalent to
self.sorted().take(k)but much more efficient.use Itertools; // A random permutation of 0..15 let numbers = vec!; let five_smallest = numbers .into_iter .k_smallest_relaxed; assert_equal;fn k_smallest_relaxed_by<F>(self, k: usize, cmp: F) -> IntoIter<Self::Item> where Self: Sized, F: FnMut(&Self::Item, &Self::Item) -> Ordering,Sort the k smallest elements into a new iterator using the provided comparison, relaxing the amount of memory required.
The sorted iterator, if directly collected to a
Vec, is converted without any extra copying or allocation cost.This corresponds to
self.sorted_by(cmp).take(k)in the same way thatk_smallest_relaxedcorresponds toself.sorted().take(k), in both semantics and complexity.use Itertools; // A random permutation of 0..15 let numbers = vec!; let five_smallest = numbers .into_iter .k_smallest_relaxed_by; assert_equal;fn k_smallest_relaxed_by_key<F, K>(self, k: usize, key: F) -> IntoIter<Self::Item> where Self: Sized, F: FnMut(&Self::Item) -> K, K: Ord,Return the elements producing the k smallest outputs of the provided function, relaxing the amount of memory required.
The sorted iterator, if directly collected to a
Vec, is converted without any extra copying or allocation cost.This corresponds to
self.sorted_by_key(key).take(k)in the same way thatk_smallest_relaxedcorresponds toself.sorted().take(k), in both semantics and complexity.use Itertools; // A random permutation of 0..15 let numbers = vec!; let five_smallest = numbers .into_iter .k_smallest_relaxed_by_key; assert_equal;fn k_largest(self, k: usize) -> IntoIter<Self::Item> where Self: Sized, Self::Item: Ord,Sort the k largest elements into a new iterator, in descending order.
The sorted iterator, if directly collected to a
Vec, is converted without any extra copying or allocation cost.It is semantically equivalent to
k_smallestwith a reversedOrd. However, this is implemented with a custom binary heap which does not have the same performance characteristics for very largeSelf::Item.use Itertools; // A random permutation of 0..15 let numbers = vec!; let five_largest = numbers .into_iter .k_largest; assert_equal;fn k_largest_by<F>(self, k: usize, cmp: F) -> IntoIter<Self::Item> where Self: Sized, F: FnMut(&Self::Item, &Self::Item) -> Ordering,Sort the k largest elements into a new iterator using the provided comparison.
The sorted iterator, if directly collected to a
Vec, is converted without any extra copying or allocation cost.Functionally equivalent to
k_smallest_bywith a reversedOrd.use Itertools; // A random permutation of 0..15 let numbers = vec!; let five_largest = numbers .into_iter .k_largest_by; assert_equal;fn k_largest_by_key<F, K>(self, k: usize, key: F) -> IntoIter<Self::Item> where Self: Sized, F: FnMut(&Self::Item) -> K, K: Ord,Return the elements producing the k largest outputs of the provided function.
The sorted iterator, if directly collected to a
Vec, is converted without any extra copying or allocation cost.Functionally equivalent to
k_smallest_by_keywith a reversedOrd.use Itertools; // A random permutation of 0..15 let numbers = vec!; let five_largest = numbers .into_iter .k_largest_by_key; assert_equal;fn k_largest_relaxed(self, k: usize) -> IntoIter<Self::Item> where Self: Sized, Self::Item: Ord,Sort the k largest elements into a new iterator, in descending order, relaxing the amount of memory required.
The sorted iterator, if directly collected to a
Vec, is converted without any extra copying or allocation cost.It is semantically equivalent to
k_smallest_relaxedwith a reversedOrd.use Itertools; // A random permutation of 0..15 let numbers = vec!; let five_largest = numbers .into_iter .k_largest_relaxed; assert_equal;fn k_largest_relaxed_by<F>(self, k: usize, cmp: F) -> IntoIter<Self::Item> where Self: Sized, F: FnMut(&Self::Item, &Self::Item) -> Ordering,Sort the k largest elements into a new iterator using the provided comparison, relaxing the amount of memory required.
The sorted iterator, if directly collected to a
Vec, is converted without any extra copying or allocation cost.Functionally equivalent to
k_smallest_relaxed_bywith a reversedOrd.use Itertools; // A random permutation of 0..15 let numbers = vec!; let five_largest = numbers .into_iter .k_largest_relaxed_by; assert_equal;fn k_largest_relaxed_by_key<F, K>(self, k: usize, key: F) -> IntoIter<Self::Item> where Self: Sized, F: FnMut(&Self::Item) -> K, K: Ord,Return the elements producing the k largest outputs of the provided function, relaxing the amount of memory required.
The sorted iterator, if directly collected to a
Vec, is converted without any extra copying or allocation cost.Functionally equivalent to
k_smallest_relaxed_by_keywith a reversedOrd.use Itertools; // A random permutation of 0..15 let numbers = vec!; let five_largest = numbers .into_iter .k_largest_relaxed_by_key; assert_equal;fn tail(self, n: usize) -> IntoIter<Self::Item> where Self: Sized,Consumes the iterator and return an iterator of the last
nelements.The iterator, if directly collected to a
VecDeque, is converted without any extra copying or allocation cost. If directly collected to aVec, it may need some data movement but no re-allocation.use ; let v = vec!; assert_equal; assert_equal; assert_equal; assert_equal; assert_equal;For double ended iterators without side-effects, you might prefer
.rev().take(n).rev()to have a similar result (lazy and non-allocating) without consuming the entire iterator.fn partition_map<A, B, F, L, R>(self, predicate: F) -> (A, B) where Self: Sized, F: FnMut(Self::Item) -> Either<L, R>, A: Default + Extend<L>, B: Default + Extend<R>,Collect all iterator elements into one of two partitions. Unlike
Iterator::partition, each partition may have a distinct type.use ; let successes_and_failures = vec!; let : = successes_and_failures .into_iter .partition_map; assert_eq!; assert_eq!;fn partition_result<A, B, T, E>(self) -> (A, B) where Self: Iterator<Item = Result<T, E>> + Sized, A: Default + Extend<T>, B: Default + Extend<E>,Partition a sequence of
Results into one list of all theOkelements and another list of all theErrelements.use Itertools; let successes_and_failures = vec!; let : = successes_and_failures .into_iter .partition_result; assert_eq!; assert_eq!;fn into_group_map<K, V>(self) -> HashMap<K, Vec<V>> where Self: Iterator<Item = (K, V)> + Sized, K: Hash + Eq,Return a
HashMapof keys mapped toVecs of values. Keys and values are taken from(Key, Value)tuple pairs yielded by the input iterator.Essentially a shorthand for
.into_grouping_map().collect::<Vec<_>>().use Itertools; let data = vec!; let lookup = data.into_iter.into_group_map; assert_eq!; assert_eq!; assert_eq!; assert_eq!;fn into_group_map_with_hasher<K, V, S>(self, hash_builder: S) -> HashMap<K, Vec<V>, S> where Self: Iterator<Item = (K, V)> + Sized, K: Hash + Eq, S: BuildHasher,Return a
HashMapof keys mapped toVecs of values, using the hash builder for hashing. See .into_group_map() for more information.Warning:
hash_builderis normally randomly generated, and is designed to allow it's users to be resistant to attacks that cause many collisions and very poor performance. Setting it manually using this function can expose a DoS attack vector.use RandomState; use Itertools; let data = vec!; let lookup = data.into_iter.into_group_map_with_hasher; assert_eq!; assert_eq!; assert_eq!; assert_eq!;fn into_group_map_by<K, V, F>(self, f: F) -> HashMap<K, Vec<V>> where Self: Iterator<Item = V> + Sized, K: Hash + Eq, F: FnMut(&V) -> K,Return a
HashMapof keys mapped toVecs of values. The key is specified in the closure. The values are taken from the input iterator.Essentially a shorthand for
.into_grouping_map_by(f).collect::<Vec<_>>().use Itertools; use HashMap; let data = vec!; let lookup: = data.clone.into_iter.into_group_map_by; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!;fn into_group_map_by_with_hasher<K, V, F, S>(self, f: F, hash_builder: S) -> HashMap<K, Vec<V>, S> where Self: Iterator<Item = V> + Sized, K: Hash + Eq, F: FnMut(&V) -> K, S: BuildHasher,Return a
HashMapof keys mapped toVecs of values, using the hash builder for hashing. See .into_group_map_by() for more information.Warning:
hash_builderis normally randomly generated, and is designed to allow it's users to be resistant to attacks that cause many collisions and very poor performance. Setting it manually using this function can expose a DoS attack vector.use Itertools; use HashMap; use RandomState; let data = vec!; let lookup: = data.clone.into_iter.into_group_map_by_with_hasher; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!;fn into_grouping_map<K, V>(self) -> GroupingMap<Self> where Self: Iterator<Item = (K, V)> + Sized, K: Hash + Eq,Constructs a
GroupingMapto be used later with one of the efficient group-and-fold operations it allows to perform.The input iterator must yield item in the form of
(K, V)where the value of typeKwill be used as key to identify the groups and the value of typeVas value for the folding operation.See
GroupingMapfor more information on what operations are available.fn into_grouping_map_with_hasher<K, V, S>(self, hash_builder: S) -> GroupingMap<Self, S> where Self: Iterator<Item = (K, V)> + Sized, K: Hash + Eq, S: BuildHasher,Constructs a
GroupingMapto be used later with one of the efficient group-and-fold operations it allows to perform, using the specified hash builder for hashing the elements. See .into_grouping_map() for more information.fn into_grouping_map_by<K, V, F>(self, key_mapper: F) -> GroupingMapBy<Self, F> where Self: Iterator<Item = V> + Sized, K: Hash + Eq, F: FnMut(&V) -> K,Constructs a
GroupingMapto be used later with one of the efficient group-and-fold operations it allows to perform.The values from this iterator will be used as values for the folding operation while the keys will be obtained from the values by calling
key_mapper.See
GroupingMapfor more information on what operations are available.fn into_grouping_map_by_with_hasher<K, V, F, S>(self, key_mapper: F, hash_builder: S) -> GroupingMapBy<Self, F, S> where Self: Iterator<Item = V> + Sized, K: Hash + Eq, F: FnMut(&V) -> K, S: BuildHasher,Constructs a
GroupingMapto be used later with one of the efficient group-and-fold operations it allows to perform, using the specified hash builder for hashing the keys. See .into_grouping_map_by() for more information.fn min_set(self) -> Vec<Self::Item> where Self: Sized, Self::Item: Ord,Return all minimum elements of an iterator.
Examples
use Itertools; let a: = ; assert_eq!; let a = ; assert_eq!; let a = ; assert_eq!; let a = ; assert_eq!;The elements can be floats but no particular result is guaranteed if an element is NaN.
fn min_set_by<F>(self, compare: F) -> Vec<Self::Item> where Self: Sized, F: FnMut(&Self::Item, &Self::Item) -> Ordering,Return all minimum elements of an iterator, as determined by the specified function.
Examples
# use Ordering; use Itertools; let a: = ; assert_eq!; let a = ; assert_eq!; let a = ; assert_eq!; let a = ; assert_eq!;The elements can be floats but no particular result is guaranteed if an element is NaN.
fn min_set_by_key<K, F>(self, key: F) -> Vec<Self::Item> where Self: Sized, K: Ord, F: FnMut(&Self::Item) -> K,Return all minimum elements of an iterator, as determined by the specified function.
Examples
use Itertools; let a: = ; assert_eq!; let a = ; assert_eq!; let a = ; assert_eq!; let a = ; assert_eq!;The elements can be floats but no particular result is guaranteed if an element is NaN.
fn max_set(self) -> Vec<Self::Item> where Self: Sized, Self::Item: Ord,Return all maximum elements of an iterator.
Examples
use Itertools; let a: = ; assert_eq!; let a = ; assert_eq!; let a = ; assert_eq!; let a = ; assert_eq!;The elements can be floats but no particular result is guaranteed if an element is NaN.
fn max_set_by<F>(self, compare: F) -> Vec<Self::Item> where Self: Sized, F: FnMut(&Self::Item, &Self::Item) -> Ordering,Return all maximum elements of an iterator, as determined by the specified function.
Examples
# use Ordering; use Itertools; let a: = ; assert_eq!; let a = ; assert_eq!; let a = ; assert_eq!; let a = ; assert_eq!;The elements can be floats but no particular result is guaranteed if an element is NaN.
fn max_set_by_key<K, F>(self, key: F) -> Vec<Self::Item> where Self: Sized, K: Ord, F: FnMut(&Self::Item) -> K,Return all maximum elements of an iterator, as determined by the specified function.
Examples
use Itertools; let a: = ; assert_eq!; let a = ; assert_eq!; let a = ; assert_eq!; let a = ; assert_eq!;The elements can be floats but no particular result is guaranteed if an element is NaN.
fn minmax(self) -> MinMaxResult<Self::Item> where Self: Sized, Self::Item: PartialOrd,Return the minimum and maximum elements in the iterator.
The return type
MinMaxResultis an enum of three variants:NoElementsif the iterator is empty.OneElement(x)if the iterator has exactly one element.MinMax(x, y)is returned otherwise, wherex <= 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,minmaxdoes1.5 * ncomparisons, and so is faster than callingminandmaxseparately which does2 * ncomparisons.Examples
use Itertools; use ; let a: = ; assert_eq!; let a = ; assert_eq!; let a = ; assert_eq!; let a = ; assert_eq!;use Itertools; use ; let a: = ; assert_eq!; let a = ; assert_eq!; let a = ; assert_eq!; let a = ; assert_eq!;The elements can be floats but no particular result is guaranteed if an element is NaN.
fn minmax_by_key<K, F>(self, key: F) -> MinMaxResult<Self::Item> where Self: Sized, K: PartialOrd, F: FnMut(&Self::Item) -> K,Return the minimum and maximum element of an iterator, as determined by the specified function.
The return value is a variant of
MinMaxResultlike for.minmax().For the minimum, the first minimal element is returned. For the maximum, the last maximal element wins. This matches the behavior of the standard
Iterator::minandIterator::maxmethods.The keys can be floats but no particular result is guaranteed if a key is NaN.
Examples
use Itertools; use ; let cmp_key = ; let a: = ; assert_eq!; let a = ; assert_eq!; let a = ; assert_eq!; let a = ; assert_eq!;fn minmax_by<F>(self, compare: F) -> MinMaxResult<Self::Item> where Self: Sized, F: FnMut(&Self::Item, &Self::Item) -> Ordering,Return the minimum and maximum element of an iterator, as determined by the specified comparison function.
The return value is a variant of
MinMaxResultlike for.minmax().For the minimum, the first minimal element is returned. For the maximum, the last maximal element wins. This matches the behavior of the standard
Iterator::minandIterator::maxmethods.Examples
use Itertools; use ; let first_item_cmp = ; let a: = ; assert_eq!; let a = ; assert_eq!; let a = ; assert_eq!; let a = ; assert_eq!;fn position_max(self) -> Option<usize> where Self: Sized, Self::Item: Ord,Return the position of the maximum element in the iterator.
If several elements are equally maximum, the position of the last of them is returned.
Examples
use Itertools; let a: = ; assert_eq!; let a = ; assert_eq!; let a = ; assert_eq!;fn position_max_by_key<K, F>(self, key: F) -> Option<usize> where Self: Sized, K: Ord, F: FnMut(&Self::Item) -> K,Return the position of the maximum element in the iterator, as determined by the specified function.
If several elements are equally maximum, the position of the last of them is returned.
Examples
use Itertools; let a: = ; assert_eq!; let a = ; assert_eq!; let a = ; assert_eq!;fn position_max_by<F>(self, compare: F) -> Option<usize> where Self: Sized, F: FnMut(&Self::Item, &Self::Item) -> Ordering,Return the position of the maximum element in the iterator, as determined by the specified comparison function.
If several elements are equally maximum, the position of the last of them is returned.
Examples
use Itertools; let a: = ; assert_eq!; let a = ; assert_eq!; let a = ; assert_eq!;fn position_min(self) -> Option<usize> where Self: Sized, Self::Item: Ord,Return the position of the minimum element in the iterator.
If several elements are equally minimum, the position of the first of them is returned.
Examples
use Itertools; let a: = ; assert_eq!; let a = ; assert_eq!; let a = ; assert_eq!;fn position_min_by_key<K, F>(self, key: F) -> Option<usize> where Self: Sized, K: Ord, F: FnMut(&Self::Item) -> K,Return the position of the minimum element in the iterator, as determined by the specified function.
If several elements are equally minimum, the position of the first of them is returned.
Examples
use Itertools; let a: = ; assert_eq!; let a = ; assert_eq!; let a = ; assert_eq!;fn position_min_by<F>(self, compare: F) -> Option<usize> where Self: Sized, F: FnMut(&Self::Item, &Self::Item) -> Ordering,Return the position of the minimum element in the iterator, as determined by the specified comparison function.
If several elements are equally minimum, the position of the first of them is returned.
Examples
use Itertools; let a: = ; assert_eq!; let a = ; assert_eq!; let a = ; assert_eq!;fn position_minmax(self) -> MinMaxResult<usize> where Self: Sized, Self::Item: PartialOrd,Return the positions of the minimum and maximum elements in the iterator.
The return type
MinMaxResultis an enum of three variants:NoElementsif the iterator is empty.OneElement(xpos)if the iterator has exactly one element.MinMax(xpos, ypos)is returned otherwise, where the element atxpos≤ the element atypos. While the referenced elements themselves may be equal,xposcannot be equal toypos.
On an iterator of length
n,position_minmaxdoes1.5 * ncomparisons, and so is faster than callingposition_minandposition_maxseparately which does2 * ncomparisons.For the minimum, if several elements are equally minimum, the position of the first of them is returned. For the maximum, if several elements are equally maximum, the position of the last of them is returned.
The elements can be floats but no particular result is guaranteed if an element is NaN.
Examples
use Itertools; use ; let a: = ; assert_eq!; let a = ; assert_eq!; let a = ; assert_eq!; let a = ; assert_eq!;fn position_minmax_by_key<K, F>(self, key: F) -> MinMaxResult<usize> where Self: Sized, K: PartialOrd, F: FnMut(&Self::Item) -> K,Return the positions of the minimum and maximum elements of an iterator, as determined by the specified function.
The return value is a variant of
MinMaxResultlike forposition_minmax.For the minimum, if several elements are equally minimum, the position of the first of them is returned. For the maximum, if several elements are equally maximum, the position of the last of them is returned.
The keys can be floats but no particular result is guaranteed if a key is NaN.
Examples
use Itertools; use ; let a: = ; assert_eq!; let a = ; assert_eq!; let a = ; assert_eq!; let a = ; assert_eq!;fn position_minmax_by<F>(self, compare: F) -> MinMaxResult<usize> where Self: Sized, F: FnMut(&Self::Item, &Self::Item) -> Ordering,Return the positions of the minimum and maximum elements of an iterator, as determined by the specified comparison function.
The return value is a variant of
MinMaxResultlike forposition_minmax.For the minimum, if several elements are equally minimum, the position of the first of them is returned. For the maximum, if several elements are equally maximum, the position of the last of them is returned.
Examples
use Itertools; use ; let a: = ; assert_eq!; let a = ; assert_eq!; let a = ; assert_eq!; let a = ; assert_eq!;fn exactly_one(self) -> Result<Self::Item, ExactlyOneError<Self>> where Self: Sized,If the iterator yields exactly one element, that element will be returned, otherwise an error will be returned containing an iterator that has the same output as the input iterator.
This provides an additional layer of validation over just calling
Iterator::next(). If your assumption that there should only be one element yielded is false this provides the opportunity to detect and handle that, preventing errors at a distance.Examples
use Itertools; assert_eq!; assert!; assert!; assert!;fn at_most_one(self) -> Result<Option<Self::Item>, ExactlyOneError<Self>> where Self: Sized,If the iterator yields no elements,
Ok(None)will be returned. If the iterator yields exactly one element, that element will be returned, otherwise an error will be returned containing an iterator that has the same output as the input iterator.This provides an additional layer of validation over just calling
Iterator::next(). If your assumption that there should be at most one element yielded is false this provides the opportunity to detect and handle that, preventing errors at a distance.Examples
use Itertools; assert_eq!; assert!; assert!; assert_eq!;fn multipeek(self) -> MultiPeek<Self> where Self: Sized,An iterator adaptor that allows the user to peek at multiple
.next()values without advancing the base iterator.Examples
use Itertools; let mut iter = .multipeek; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!;fn counts(self) -> HashMap<Self::Item, usize> where Self: Sized, Self::Item: Eq + Hash,Collect the items in this iterator and return a
HashMapwhich contains each item that appears in the iterator and the number of times it appears.Examples
# use Itertools; let counts = .iter.counts; assert_eq!; assert_eq!; assert_eq!; assert_eq!;fn counts_with_hasher<S>(self, hash_builder: S) -> HashMap<Self::Item, usize, S> where Self: Sized, Self::Item: Eq + Hash, S: BuildHasher,Collect the items in this iterator and return a
HashMapthe same way .counts() does, but use the specified hash builder for hashing.fn counts_by<K, F>(self, f: F) -> HashMap<K, usize> where Self: Sized, K: Eq + Hash, F: FnMut(Self::Item) -> K,Collect the items in this iterator and return a
HashMapwhich contains each item that appears in the iterator and the number of times it appears, determining identity using a keying function.# use Itertools; let characters = vec!; let first_name_frequency = characters .into_iter .counts_by; assert_eq!; assert_eq!; assert_eq!;fn counts_by_with_hasher<K, F, S>(self, f: F, hash_builder: S) -> HashMap<K, usize, S> where Self: Sized, K: Eq + Hash, F: FnMut(Self::Item) -> K, S: BuildHasher,Collect the items in this iterator and return a
HashMapthe same way .counts_by() does, but use the specified hash builder for hashing.fn multiunzip<FromI>(self) -> FromI where Self: Sized + MultiUnzip<FromI>,Converts an iterator of tuples into a tuple of containers.
It consumes an entire iterator of n-ary tuples, producing
ncollections, one for each column.This function is, in some sense, the opposite of
multizip.use Itertools; let inputs = vec!; let : = inputs .into_iter .multiunzip; assert_eq!; assert_eq!; assert_eq!;fn try_len(&self) -> Result<usize, (usize, Option<usize>)>Returns the length of the iterator if one exists. Otherwise return
self.size_hint().Fallible
ExactSizeIterator::len.Inherits guarantees and restrictions from
Iterator::size_hint.use Itertools; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!;fn strip_prefix<Prefix>(self, prefix: Prefix) -> Result<Self, StripPrefixError<Self, Prefix::IntoIter, Self::Item>> where Self: Sized, Prefix: IntoIterator, Self::Item: PartialEq<Prefix::Item>,Removes a prefix from the iterator, returning the rest.
If
selfbegins with all the items yielded byprefix(in order), this returnsOkof the iterator advanced past that prefix. Otherwise it returnsErr(StripPrefixError { .. })exposing the partially-consumed iterator, the remaining prefix, and the items that failed to match, so callers can recover progress made before the mismatch.See
strip_prefix_byfor a variant taking an explicit equality predicate.use Itertools; let ok = .strip_prefix.map.ok; assert_eq!; assert!; let empty = .strip_prefix.map.ok; assert_eq!;fn strip_prefix_by<Prefix, F>(self, prefix: Prefix, eq: F) -> Result<Self, StripPrefixError<Self, Prefix::IntoIter, Self::Item>> where Self: Sized, Prefix: IntoIterator, F: FnMut(&Self::Item, &Prefix::Item) -> bool,Removes a prefix from the iterator using
eqto compare items.If
selfbegins with all the items yielded byprefix(in order, as judged byeq), this returnsOkof the iterator advanced past that prefix. Otherwise it returnsErr(StripPrefixError { .. }), allowing the prefix items to have a different type thanSelf::Item.use Itertools; let path = ; let stripped = path.iter.strip_prefix_by; assert_eq!;
Implementors
impl<T> Itertools for ArrayWindows<I, N> where T: Iterator + ?Sized,impl<T> Itertools for Batching<I, F> where T: Iterator + ?Sized,impl<T> Itertools for Chunk<'a, I> where T: Iterator + ?Sized,impl<T> Itertools for Chunks<'a, I> where T: Iterator + ?Sized,impl<T> Itertools for CircularArrayWindows<I, N> where T: Iterator + ?Sized,impl<T> Itertools for CircularTupleWindows<I, T> where T: Iterator + ?Sized,impl<T> Itertools for ExactlyOneError<I> where T: Iterator + ?Sized,impl<T> Itertools for FilterMapOk<I, F> where T: Iterator + ?Sized,impl<T> Itertools for FilterOk<I, F> where T: Iterator + ?Sized,impl<T> Itertools for FlattenOk<I, T, E> where T: Iterator + ?Sized,impl<T> Itertools for Group<'a, K, I, F> where T: Iterator + ?Sized,impl<T> Itertools for Groups<'a, K, I, F> where T: Iterator + ?Sized,impl<T> Itertools for Interleave<I, J> where T: Iterator + ?Sized,impl<T> Itertools for InterleaveShortest<I, J> where T: Iterator + ?Sized,impl<T> Itertools for IntersperseWith<I, ElemF> where T: Iterator + ?Sized,impl<T> Itertools for Iterate<St, F> where T: Iterator + ?Sized,impl<T> Itertools for KMergeBy<I, F> where T: Iterator + ?Sized,impl<T> Itertools for MergeBy<I, J, F> where T: Iterator + ?Sized,impl<T> Itertools for MultiPeek<I> where T: Iterator + ?Sized,impl<T> Itertools for MultiProduct<I> where T: Iterator + ?Sized,impl<T> Itertools for PadUsing<I, F> where T: Iterator + ?Sized,impl<T> Itertools for PeekNth<I> where T: Iterator + ?Sized,impl<T> Itertools for PeekingTakeWhile<'a, I, F> where T: Iterator + ?Sized,impl<T> Itertools for Permutations<I> where T: Iterator + ?Sized,impl<T> Itertools for Positions<I, F> where T: Iterator + ?Sized,impl<T> Itertools for Powerset<I> where T: Iterator + ?Sized,impl<T> Itertools for ProcessResults<'a, I, E> where T: Iterator + ?Sized,impl<T> Itertools for Product<I, J> where T: Iterator + ?Sized,impl<T> Itertools for PutBack<I> where T: Iterator + ?Sized,impl<T> Itertools for PutBackN<I> where T: Iterator + ?Sized,impl<T> Itertools for RcIter<I> where T: Iterator + ?Sized,impl<T> Itertools for RepeatN<A> where T: Iterator + ?Sized,impl<T> Itertools for T where T: Iterator + ?Sized,impl<T> Itertools for TakeWhileInclusive<I, F> where T: Iterator + ?Sized,impl<T> Itertools for TakeWhileRef<'a, I, F> where T: Iterator + ?Sized,impl<T> Itertools for Tee<I> where T: Iterator + ?Sized,impl<T> Itertools for TupleBuffer<T> where T: Iterator + ?Sized,impl<T> Itertools for TupleCombinations<I, T> where T: Iterator + ?Sized,impl<T> Itertools for TupleWindows<I, T> where T: Iterator + ?Sized,impl<T> Itertools for Tuples<I, T> where T: Iterator + ?Sized,impl<T> Itertools for Unfold<St, F> where T: Iterator + ?Sized,impl<T> Itertools for Unique<I, S> where T: Iterator + ?Sized,impl<T> Itertools for UniqueBy<I, V, F, S> where T: Iterator + ?Sized,impl<T> Itertools for Update<I, F> where T: Iterator + ?Sized,impl<T> Itertools for WhileSome<I> where T: Iterator + ?Sized,impl<T> Itertools for WithPosition<I> where T: Iterator + ?Sized,impl<T> Itertools for Zip<T> where T: Iterator + ?Sized,impl<T> Itertools for ZipEq<I, J> where T: Iterator + ?Sized,impl<T> Itertools for ZipLongest<T, U> where T: Iterator + ?Sized,