Trait Itertools
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: Self, other: J) -> Interleave<Self, <J as >::IntoIter> where J: IntoIterator<Item = <Self as >::Item>, Self: SizedAlternate 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: Self, other: J) -> InterleaveShortest<Self, <J as >::IntoIter> where J: IntoIterator<Item = <Self as >::Item>, Self: SizedAlternate 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: Self, element: <Self as >::Item) -> Intersperse<Self> where Self: Sized, <Self as >::Item: CloneAn 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: Self, element: F) -> IntersperseWith<Self, F> where Self: Sized, F: FnMut() -> <Self as >::ItemAn 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: Self, index: R) -> <R as >::Output where Self: Sized, R: traits::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: Self, other: J) -> ZipLongest<Self, <J as >::IntoIter> where J: IntoIterator, Self: SizedCreate 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: Self, other: J) -> ZipEq<Self, <J as >::IntoIter> where J: IntoIterator, Self: SizedCreate 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.
fn batching<B, F>(self: Self, f: F) -> Batching<Self, F> where F: FnMut(&mut Self) -> Option<B>, Self: SizedA “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: Self, key: F) -> ChunkBy<K, Self, F> where Self: Sized, F: FnMut(&<Self as >::Item) -> K, K: PartialEqReturn 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: Self, key: F) -> ChunkBy<K, Self, F> where Self: Sized, F: FnMut(&<Self as >::Item) -> K, K: PartialEqSee
.chunk_by().fn chunks(self: Self, size: usize) -> IntoChunks<Self> where Self: SizedReturn 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.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.chunksfn tuple_windows<T>(self: Self) -> TupleWindows<Self, T> where Self: Sized + Iterator<Item = <T as >::Item>, T: traits::HomogeneousTuple, <T as >::Item: CloneReturn 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: Self) -> CircularTupleWindows<Self, T> where Self: Sized + Clone + Iterator<Item = <T as >::Item> + ExactSizeIterator, T: tuple_impl::TupleCollect + Clone, <T as >::Item: CloneReturn 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: Self) -> Tuples<Self, T> where Self: Sized + Iterator<Item = <T as >::Item>, T: traits::HomogeneousTupleReturn 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 tee(self: Self) -> (Tee<Self>, Tee<Self>) where Self: Sized, <Self as >::Item: CloneSplit into an iterator pair that both yield all elements from the original iterator.
Note: If the iterator is clonable, 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: Self) -> MapInto<Self, R> where Self: Sized, <Self as >::Item: Into<R>Convert each item of the iterator using the
Intotrait.use Itertools; ..collect_vec;fn map_ok<F, T, U, E>(self: Self, f: F) -> MapOk<Self, F> where Self: Iterator<Item = Result<T, E>> + Sized, F: FnMut(T) -> UReturn 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: Self, f: F) -> FilterOk<Self, F> where Self: Iterator<Item = Result<T, E>> + Sized, F: FnMut(&T) -> boolReturn 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: 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: Self) -> FlattenOk<Self, T, E> where Self: Iterator<Item = Result<T, E>> + Sized, T: IntoIteratorReturn 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: 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 iself.
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: Self, other: J) -> Merge<Self, <J as >::IntoIter> where Self: Sized, <Self as >::Item: PartialOrd, J: IntoIterator<Item = <Self as >::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: Self, other: J, is_first: F) -> MergeBy<Self, <J as >::IntoIter, F> where Self: Sized, J: IntoIterator<Item = <Self as >::Item>, F: FnMut(&<Self as >::Item, &<Self as >::Item) -> boolReturn 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: Self, other: J, cmp_fn: F) -> MergeJoinBy<Self, <J as >::IntoIter, F> where J: IntoIterator, F: FnMut(&<Self as >::Item, &<J as >::Item) -> T, Self: SizedCreate 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 Itertools; use ; 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 Itertools; use ; let a = vec!.into_iter; let b = .step_by; assert_equal;- Emit
fn kmerge(self: Self) -> KMerge<<<Self as >::Item as IntoIterator>::IntoIter> where Self: Sized, <Self as >::Item: IntoIterator, <<Self as >::Item as IntoIterator>::Item: PartialOrdReturn an iterator adaptor that flattens an iterator of iterators by merging them in ascending order.
If all 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 c = .step_by; let it = vec!.into_iter.kmerge; assert_equal;fn kmerge_by<F>(self: Self, first: F) -> KMergeBy<<<Self as >::Item as IntoIterator>::IntoIter, F> where Self: Sized, <Self as >::Item: IntoIterator, F: FnMut(&<<Self as >::Item as IntoIterator>::Item, &<<Self as >::Item as IntoIterator>::Item) -> boolReturn 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: Self, other: J) -> Product<Self, <J as >::IntoIter> where Self: Sized, <Self as >::Item: Clone, J: IntoIterator, <J as >::IntoIter: CloneReturn 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: Self) -> MultiProduct<<<Self as >::Item as IntoIterator>::IntoIter> where Self: Sized, <Self as >::Item: IntoIterator, <<Self as >::Item as IntoIterator>::IntoIter: Clone, <<Self as >::Item as IntoIterator>::Item: CloneReturn 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: Self, f: F) -> Coalesce<Self, F> where Self: Sized, F: FnMut(<Self as >::Item, <Self as >::Item) -> Result<<Self as >::Item, (<Self as >::Item, <Self as >::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: Self) -> Dedup<Self> where Self: Sized, <Self as >::Item: PartialEqRemove 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: Self, cmp: Cmp) -> DedupBy<Self, Cmp> where Self: Sized, Cmp: FnMut(&<Self as >::Item, &<Self as >::Item) -> boolRemove 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: Self) -> DedupWithCount<Self> where Self: SizedRemove 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: Self, cmp: Cmp) -> DedupByWithCount<Self, Cmp> where Self: Sized, Cmp: FnMut(&<Self as >::Item, &<Self as >::Item) -> boolRemove 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: Self) -> Duplicates<Self> where Self: Sized, <Self as >::Item: Eq + HashReturn 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_by<V, F>(self: Self, f: F) -> DuplicatesBy<Self, V, F> where Self: Sized, V: Eq + Hash, F: FnMut(&<Self as >::Item) -> VReturn 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 unique(self: Self) -> Unique<Self> where Self: Sized, <Self as >::Item: Clone + Eq + HashReturn 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_by<V, F>(self: Self, f: F) -> UniqueBy<Self, V, F> where Self: Sized, V: Eq + Hash, F: FnMut(&<Self as >::Item) -> VReturn 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 peeking_take_while<F>(self: &mut Self, accept: F) -> PeekingTakeWhile<'_, Self, F> where Self: Sized + PeekingNext, F: FnMut(&<Self as >::Item) -> boolReturn 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>(self: &mut Self, accept: F) -> TakeWhileRef<'_, Self, F> where Self: Clone, F: FnMut(&<Self as >::Item) -> boolReturn 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: Self, accept: F) -> TakeWhileInclusive<Self, F> where Self: Sized, F: FnMut(&<Self as >::Item) -> boolReturns 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_clonable_items: = vec! .into_iter .map .collect; let filtered: = non_clonable_items .into_iter .take_while_inclusive .collect; let expected: = vec!.into_iter.map.collect; assert_eq!;fn while_some<A>(self: 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: Self) -> TupleCombinations<Self, T> where Self: Sized + Clone, <Self as >::Item: Clone, T: adaptors::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<K: usize>(self: Self) -> ArrayCombinations<Self, K> where Self: Sized + Clone, <Self as >::Item: CloneReturn 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: Self, k: usize) -> Combinations<Self> where Self: Sized, <Self as >::Item: CloneReturn 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: Self, k: usize) -> CombinationsWithReplacement<Self> where Self: Sized, <Self as >::Item: CloneReturn 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 permutations(self: Self, k: usize) -> Permutations<Self> where Self: Sized, <Self as >::Item: CloneReturn 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: Self) -> Powerset<Self> where Self: Sized, <Self as >::Item: CloneReturn 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: Self, min: usize, f: F) -> PadUsing<Self, F> where Self: Sized, F: FnMut(usize) -> <Self as >::ItemReturn 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: Self) -> WithPosition<Self> where Self: SizedReturn 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: Self, predicate: P) -> Positions<Self, P> where Self: Sized, P: FnMut(<Self as >::Item) -> boolReturn 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: Self, updater: F) -> Update<Self, F> where Self: Sized, F: FnMut(&mut <Self as >::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<N: usize>(self: &mut Self) -> Option<[<Self as >::Item; N]> where Self: SizedAdvances 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<N: usize>(self: Self) -> Option<[<Self as >::Item; N]> where Self: SizedCollects 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>(self: &mut Self) -> Option<T> where Self: Sized + Iterator<Item = <T as >::Item>, T: traits::HomogeneousTupleAdvances 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: Self) -> Option<T> where Self: Sized + Iterator<Item = <T as >::Item>, T: traits::HomogeneousTupleCollects 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>(self: &mut Self, pred: P) -> Option<(usize, <Self as >::Item)> where P: FnMut(&<Self as >::Item) -> boolFind 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: Self, predicate: P) -> Option<<Self as >::Item> where Self: Sized, P: FnMut(&<Self as >::Item) -> boolFind 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: Self, predicate: P) -> Option<<Self as >::Item> where Self: Sized, P: FnMut(&<Self as >::Item) -> boolFind 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>(self: &mut Self, query: &Q) -> bool where Self: Sized, <Self as >::Item: Borrow<Q>, Q: PartialEq + ?SizedReturns
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(self: &mut Self) -> bool where Self: Sized, <Self as >::Item: PartialEqCheck 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(self: &mut Self) -> Result<<Self as >::Item, Option<(<Self as >::Item, <Self as >::Item)>> where Self: Sized, <Self as >::Item: PartialEqIf 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 Itertools; let data = vec!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; let data : = None; assert_eq!;fn all_unique(self: &mut Self) -> bool where Self: Sized, <Self as >::Item: Eq + HashCheck 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 dropping(self: Self, n: usize) -> Self where Self: SizedConsume 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: Self, n: usize) -> Self where Self: Sized + DoubleEndedIteratorConsume 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) -> <Self as >::Item where Self: Sized, <Self as >::Item: Extend<<<Self as Iterator>::Item as IntoIterator>::Item> + IntoIterator + DefaultCombine 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: Self) -> Vec<<Self as >::Item> where Self: Sized.collect_vec()is simply a type specialization ofIterator::collect, for convenience.fn try_collect<T, U, E>(self: 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 ; use Itertools; # let _ = do_stuff;fn set_from<'a, A: 'a, J>(self: &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(self: &mut Self, sep: &str) -> String where <Self as >::Item: std::fmt::DisplayCombine all iterator elements into one
String, separated bysep.Use the
Displayimplementation of each element.use Itertools; assert_eq!; assert_eq!;fn format(self: Self, sep: &str) -> Format<'_, Self> where Self: SizedFormat 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: Self, sep: &str, format: F) -> FormatWith<'_, Self, F> where Self: Sized, F: FnMut(<Self as >::Item, &mut dyn FnMut(&dyn fmt::Display) -> fmt::Result) -> fmt::ResultFormat 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>(self: &mut Self, start: B, f: F) -> Result<B, E> where Self: Iterator<Item = Result<A, E>>, F: FnMut(B, A) -> BFold
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 Add; use Itertools; let values = ; assert_eq!; assert!;fn fold_options<A, B, F>(self: &mut Self, start: B, f: F) -> Option<B> where Self: Iterator<Item = Option<A>>, F: FnMut(B, A) -> BFold
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 Add; use Itertools; let mut values = vec!.into_iter; assert_eq!; let mut more_values = vec!.into_iter; assert!; assert_eq!;fn fold1<F>(self: Self, f: F) -> Option<<Self as >::Item> where F: FnMut(<Self as >::Item, <Self as >::Item) -> <Self as >::Item, Self: SizedAccumulator 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: Self, f: F) -> Option<<Self as >::Item> where F: FnMut(<Self as >::Item, <Self as >::Item) -> <Self as >::Item, Self: SizedAccumulate 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: Self, f: F) -> Option<<Self as >::Item> where F: FnMut(<Self as >::Item, <Self as >::Item) -> <Self as >::Item, Self: SizedSee
.tree_reduce().fn fold_while<B, F>(self: &mut Self, init: B, f: F) -> FoldWhile<B> where Self: Sized, F: FnMut(B, <Self as >::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 Itertools; use ; 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: Self) -> Option<S> where Self: Sized, S: std::iter::Sum<<Self as >::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: Self) -> Option<P> where Self: Sized, P: std::iter::Product<<Self as >::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: Self) -> alloc::vec::IntoIter<<Self as >::Item> where Self: Sized, <Self as >::Item: OrdSort 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: Self, cmp: F) -> alloc::vec::IntoIter<<Self as >::Item> where Self: Sized, F: FnMut(&<Self as >::Item, &<Self as >::Item) -> OrderingSort 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: Self, f: F) -> alloc::vec::IntoIter<<Self as >::Item> where Self: Sized, K: Ord, F: FnMut(&<Self as >::Item) -> KSort 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: Self) -> alloc::vec::IntoIter<<Self as >::Item> where Self: Sized, <Self as >::Item: OrdSort 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: Self, cmp: F) -> alloc::vec::IntoIter<<Self as >::Item> where Self: Sized, F: FnMut(&<Self as >::Item, &<Self as >::Item) -> OrderingSort 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: Self, f: F) -> alloc::vec::IntoIter<<Self as >::Item> where Self: Sized, K: Ord, F: FnMut(&<Self as >::Item) -> KSort 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: Self, f: F) -> alloc::vec::IntoIter<<Self as >::Item> where Self: Sized, K: Ord, F: FnMut(&<Self as >::Item) -> KSort 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: Self, k: usize) -> alloc::vec::IntoIter<<Self as >::Item> where Self: Sized, <Self as >::Item: OrdSort 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: Self, k: usize, cmp: F) -> alloc::vec::IntoIter<<Self as >::Item> where Self: Sized, F: FnMut(&<Self as >::Item, &<Self as >::Item) -> OrderingSort 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: Self, k: usize, key: F) -> alloc::vec::IntoIter<<Self as >::Item> where Self: Sized, F: FnMut(&<Self as >::Item) -> K, K: OrdReturn 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: Self, k: usize) -> alloc::vec::IntoIter<<Self as >::Item> where Self: Sized, <Self as >::Item: OrdSort 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: Self, k: usize, cmp: F) -> alloc::vec::IntoIter<<Self as >::Item> where Self: Sized, F: FnMut(&<Self as >::Item, &<Self as >::Item) -> OrderingSort 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: Self, k: usize, key: F) -> alloc::vec::IntoIter<<Self as >::Item> where Self: Sized, F: FnMut(&<Self as >::Item) -> K, K: OrdReturn 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: Self, k: usize) -> alloc::vec::IntoIter<<Self as >::Item> where Self: Sized, <Self as >::Item: OrdSort 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: Self, k: usize, cmp: F) -> alloc::vec::IntoIter<<Self as >::Item> where Self: Sized, F: FnMut(&<Self as >::Item, &<Self as >::Item) -> OrderingSort 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: Self, k: usize, key: F) -> alloc::vec::IntoIter<<Self as >::Item> where Self: Sized, F: FnMut(&<Self as >::Item) -> K, K: OrdReturn 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: Self, k: usize) -> alloc::vec::IntoIter<<Self as >::Item> where Self: Sized, <Self as >::Item: OrdSort 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: Self, k: usize, cmp: F) -> alloc::vec::IntoIter<<Self as >::Item> where Self: Sized, F: FnMut(&<Self as >::Item, &<Self as >::Item) -> OrderingSort 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: Self, k: usize, key: F) -> alloc::vec::IntoIter<<Self as >::Item> where Self: Sized, F: FnMut(&<Self as >::Item) -> K, K: OrdReturn 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: Self, n: usize) -> alloc::collections::vec_deque::IntoIter<<Self as >::Item> where Self: SizedConsumes 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: Self, predicate: F) -> (A, B) where Self: Sized, F: FnMut(<Self as >::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: 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: Self) -> HashMap<K, Vec<V>> where Self: Iterator<Item = (K, V)> + Sized, K: Hash + EqReturn 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_by<K, V, F>(self: Self, f: F) -> HashMap<K, Vec<V>> where Self: Iterator<Item = V> + Sized, K: Hash + Eq, F: FnMut(&V) -> KReturn 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_grouping_map<K, V>(self: Self) -> GroupingMap<Self> where Self: Iterator<Item = (K, V)> + Sized, K: Hash + EqConstructs 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 informations on what operations are available.fn into_grouping_map_by<K, V, F>(self: Self, key_mapper: F) -> GroupingMapBy<Self, F> where Self: Iterator<Item = V> + Sized, K: Hash + Eq, F: FnMut(&V) -> KConstructs 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 informations on what operations are available.fn min_set(self: Self) -> Vec<<Self as >::Item> where Self: Sized, <Self as >::Item: OrdReturn 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: Self, compare: F) -> Vec<<Self as >::Item> where Self: Sized, F: FnMut(&<Self as >::Item, &<Self as >::Item) -> OrderingReturn 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: Self, key: F) -> Vec<<Self as >::Item> where Self: Sized, K: Ord, F: FnMut(&<Self as >::Item) -> KReturn 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: Self) -> Vec<<Self as >::Item> where Self: Sized, <Self as >::Item: OrdReturn 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: Self, compare: F) -> Vec<<Self as >::Item> where Self: Sized, F: FnMut(&<Self as >::Item, &<Self as >::Item) -> OrderingReturn 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: Self, key: F) -> Vec<<Self as >::Item> where Self: Sized, K: Ord, F: FnMut(&<Self as >::Item) -> KReturn 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: Self) -> MinMaxResult<<Self as >::Item> where Self: Sized, <Self as >::Item: PartialOrdReturn 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!;The elements can be floats but no particular result is guaranteed if an element is NaN.
fn minmax_by_key<K, F>(self: Self, key: F) -> MinMaxResult<<Self as >::Item> where Self: Sized, K: PartialOrd, F: FnMut(&<Self as >::Item) -> KReturn 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.
fn minmax_by<F>(self: Self, compare: F) -> MinMaxResult<<Self as >::Item> where Self: Sized, F: FnMut(&<Self as >::Item, &<Self as >::Item) -> OrderingReturn 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.fn position_max(self: Self) -> Option<usize> where Self: Sized, <Self as >::Item: OrdReturn 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: Self, key: F) -> Option<usize> where Self: Sized, K: Ord, F: FnMut(&<Self as >::Item) -> KReturn 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: Self, compare: F) -> Option<usize> where Self: Sized, F: FnMut(&<Self as >::Item, &<Self as >::Item) -> OrderingReturn 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: Self) -> Option<usize> where Self: Sized, <Self as >::Item: OrdReturn 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: Self, key: F) -> Option<usize> where Self: Sized, K: Ord, F: FnMut(&<Self as >::Item) -> KReturn 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: Self, compare: F) -> Option<usize> where Self: Sized, F: FnMut(&<Self as >::Item, &<Self as >::Item) -> OrderingReturn 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: Self) -> MinMaxResult<usize> where Self: Sized, <Self as >::Item: PartialOrdReturn 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: Self, key: F) -> MinMaxResult<usize> where Self: Sized, K: PartialOrd, F: FnMut(&<Self as >::Item) -> KReturn the postions 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: Self, compare: F) -> MinMaxResult<usize> where Self: Sized, F: FnMut(&<Self as >::Item, &<Self as >::Item) -> OrderingReturn the postions 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: Self) -> Result<<Self as >::Item, ExactlyOneError<Self>> where Self: SizedIf 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: Self) -> Result<Option<<Self as >::Item>, ExactlyOneError<Self>> where Self: SizedIf 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: Self) -> MultiPeek<Self> where Self: SizedAn 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: Self) -> HashMap<<Self as >::Item, usize> where Self: Sized, <Self as >::Item: Eq + HashCollect 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_by<K, F>(self: Self, f: F) -> HashMap<K, usize> where Self: Sized, K: Eq + Hash, F: FnMut(<Self as >::Item) -> KCollect 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 multiunzip<FromI>(self: 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: &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!;
Implementors
impl<T> Itertools for RcIter<I>impl<T> Itertools for Powerset<I>impl<T> Itertools for TakeWhileRef<'a, I, F>impl<T> Itertools for Iterate<St, F>impl<T> Itertools for Update<I, F>impl<T> Itertools for Groups<'a, K, I, F>impl<T> Itertools for KMergeBy<I, F>impl<T> Itertools for CircularTupleWindows<I, T>impl<T> Itertools for PutBack<I>impl<T> Itertools for FilterMapOk<I, F>impl<T> Itertools for Group<'a, K, I, F>impl<T> Itertools for ZipEq<I, J>impl<T> Itertools for TupleBuffer<T>impl<T> Itertools for MultiPeek<I>impl<T> Itertools for TakeWhileInclusive<I, F>impl<T> Itertools for Positions<I, F>impl<T> Itertools for Zip<T>impl<T> Itertools for TupleCombinations<I, T>impl<T> Itertools for TupleWindows<I, T>impl<T> Itertools for PeekNth<I>impl<T> Itertools for Timpl<T> Itertools for Unique<I>impl<T> Itertools for UniqueBy<I, V, F>impl<T> Itertools for MultiProduct<I>impl<T> Itertools for Tee<I>impl<T> Itertools for WithPosition<I>impl<T> Itertools for RepeatN<A>impl<T> Itertools for InterleaveShortest<I, J>impl<T> Itertools for PeekingTakeWhile<'a, I, F>impl<T> Itertools for WhileSome<I>impl<T> Itertools for ProcessResults<'a, I, E>impl<T> Itertools for FilterOk<I, F>impl<T> Itertools for PadUsing<I, F>impl<T> Itertools for ZipLongest<T, U>impl<T> Itertools for Batching<I, F>impl<T> Itertools for Unfold<St, F>impl<T> Itertools for Product<I, J>impl<T> Itertools for Chunk<'a, I>impl<T> Itertools for MergeBy<I, J, F>impl<T> Itertools for IntersperseWith<I, ElemF>impl<T> Itertools for Permutations<I>impl<T> Itertools for FlattenOk<I, T, E>impl<T> Itertools for Tuples<I, T>impl<T> Itertools for Chunks<'a, I>impl<T> Itertools for PutBackN<I>impl<T> Itertools for CombinationsWithReplacement<I>impl<T> Itertools for Interleave<I, J>impl<T> Itertools for ExactlyOneError<I>