core::iter
On this page
- Structs
Peekable1TryPeekable1- Traits
Iterator53allanychainchunkscollectcombinationscountcountscycleenumerateexact_lenfilterfindflat_mapflattenfoldfor_eachgroup_byinspectlastmapmaxmax_by_keyminmin_by_keynextnthpairwisepartitionpeekablepermutationspositionproductreducerevscanskipskip_whilesortedsorted_bysorted_by_keystep_bysumtaketake_whileto_dictto_listto_settry_foldtry_for_eachunzipwindowszip
ExactSizeIterator1IntoIterator1FromIterator1- Functions
Iterator + IntoIterator + FromIterator.
for pat in xs desugars to
let it = xs.into_iter();
loop { match it.next() { v: T => { let pat = v; body } none => break } }
(integer ranges and arrays keep a direct counting loop).
next takes ref self: an iterator's state lives in its
fields, written back into the caller's binding after each call.
Structs
ChainSource
pub struct Chain<I, J>The iterator chain returns: every item of the first, then of the
second.
Trait implementations 3
impl<I, J> Debug for Chain<I, J>impl<I: Iterator, J: Iterator<Item = I::Item>> Iterator for Chain<I, J>impl<I: DoubleEndedIterator, J: DoubleEndedIterator> DoubleEndedIterator for Chain<I, J>
ChunksSource
pub struct Chunks<I>The iterator chunks returns: lists of size items, the last one
shorter when the items run out.
Trait implementations 3
impl<I> Debug for Chunks<I>impl<I: Iterator> Iterator for Chunks<I>impl<I: ExactSizeIterator> ExactSizeIterator for Chunks<I>
CombinationsSource
pub struct Combinations<T>The iterator combinations returns: each choice of k items as a
list.
CycleSource
pub struct Cycle<I>The iterator cycle returns: the items over and over; empty if the
source is.
EnumerateSource
pub struct Enumerate<I>The iterator enumerate returns: (index, item) pairs from the
start index.
Trait implementations 4
impl<I> Debug for Enumerate<I>impl<I: Iterator> Iterator for Enumerate<I>impl<I: ExactSizeIterator> ExactSizeIterator for Enumerate<I>impl<I: DoubleEndedIterator + ExactSizeIterator> DoubleEndedIterator for Enumerate<I>
FilterSource
pub struct Filter<I>The iterator filter returns: the items pred accepts, in order.
Trait implementations 3
impl<I: Iterator> Debug for Filter<I>impl<I: Iterator> Iterator for Filter<I>impl<I: DoubleEndedIterator> DoubleEndedIterator for Filter<I>
FlatMapSource
pub struct FlatMap<I, U>flat_map: cur is the inner iterator being drained.
Trait implementations 2
impl<I: Iterator, U: IntoIterator> Debug for FlatMap<I, U>impl<I: Iterator, U: IntoIterator> Iterator for FlatMap<I, U>
FlattenSource
pub struct Flatten<I>flatten: cur is the inner iterator being drained.
Trait implementations 2
impl<I: Iterator> Debug for Flatten<I> where I::Item: IntoIteratorimpl<I: Iterator> Iterator for Flatten<I> where I::Item: IntoIterator
InspectIterSource
pub struct InspectIter<I>inspect's adapter (Inspect is the reflection trait).
Trait implementations 4
impl<I: Iterator> Debug for InspectIter<I>impl<I: Iterator> Iterator for InspectIter<I>impl<I: DoubleEndedIterator> DoubleEndedIterator for InspectIter<I>impl<I: ExactSizeIterator> ExactSizeIterator for InspectIter<I>
MapSource
pub struct Map<I, U>The iterator map returns: f of each item.
Trait implementations 4
impl<I: Iterator, U> Debug for Map<I, U>impl<I: Iterator, U> Iterator for Map<I, U>impl<I: DoubleEndedIterator, U> DoubleEndedIterator for Map<I, U>impl<I: ExactSizeIterator, U> ExactSizeIterator for Map<I, U>
PairwiseSource
pub struct Pairwise<I>The iterator pairwise returns: each item with the next.
PeekableSource
pub struct Peekable<I>The iterator peekable returns: peek looks at the next item
without taking it.
The next item, left in place for the following
next.
PermutationsSource
pub struct Permutations<T>The iterator permutations returns: each ordering of k items as a
list.
RevSource
pub struct Rev<I>The iterator rev returns: the items from the back.
Trait implementations 4
impl<I> Debug for Rev<I>impl<I: DoubleEndedIterator> Iterator for Rev<I>impl<I: DoubleEndedIterator> DoubleEndedIterator for Rev<I>impl<I: DoubleEndedIterator + ExactSizeIterator> ExactSizeIterator for Rev<I>
ScanSource
pub struct Scan<I, S>The iterator scan returns: each running state of f.
Trait implementations 3
impl<I: Iterator, S> Debug for Scan<I, S>impl<I: Iterator, S> Iterator for Scan<I, S>impl<I: ExactSizeIterator, S> ExactSizeIterator for Scan<I, S>
SkipSource
pub struct Skip<I>The iterator skip returns: all but the first n items.
Trait implementations 4
impl<I> Debug for Skip<I>impl<I: Iterator> Iterator for Skip<I>impl<I: ExactSizeIterator> ExactSizeIterator for Skip<I>impl<I: DoubleEndedIterator + ExactSizeIterator> DoubleEndedIterator for Skip<I>
SkipWhileSource
pub struct SkipWhile<I>The iterator skip_while returns: items from the first that pred
rejects on.
StepBySource
pub struct StepBy<I>The iterator step_by returns: the first item, then every stepth.
Trait implementations 4
impl<I> Debug for StepBy<I>impl<I: Iterator> Iterator for StepBy<I>impl<I: ExactSizeIterator> ExactSizeIterator for StepBy<I>impl<I: DoubleEndedIterator + ExactSizeIterator> DoubleEndedIterator for StepBy<I>
TakeSource
pub struct Take<I>The iterator take returns: at most the first n items.
Trait implementations 4
impl<I> Debug for Take<I>impl<I: Iterator> Iterator for Take<I>impl<I: ExactSizeIterator> ExactSizeIterator for Take<I>impl<I: DoubleEndedIterator + ExactSizeIterator> DoubleEndedIterator for Take<I>
TakeWhileSource
pub struct TakeWhile<I>The iterator take_while returns: items up to the first that pred
rejects.
WindowsSource
pub struct Windows<I>The iterator windows returns: every run of size consecutive items,
overlapping, as lists.
ZipSource
pub struct Zip<I, J>The iterator zip returns: pairs of items; ends with the shorter
side.
Trait implementations 4
impl<I, J> Debug for Zip<I, J>impl<I: Iterator, J: Iterator> Iterator for Zip<I, J>impl<I: ExactSizeIterator, J: ExactSizeIterator> ExactSizeIterator for Zip<I, J>impl<I: DoubleEndedIterator + ExactSizeIterator, J: DoubleEndedIterator + ExactSizeIterator> DoubleEndedIterator for Zip<I, J>
FromResultsSource
pub struct FromResults<I, T, E>The TryIterator iter::from_results returns: an Err item is
thrown.
InfallibleSource
pub struct Infallible<I, E>The TryIterator iter::infallible returns: the items, never an
error.
Trait implementations 2
impl<I: Iterator, E> Debug for Infallible<I, E>impl<I: Iterator, E> TryIterator for Infallible<I, E>
MapErrSource
pub struct MapErr<I, E2>The iterator map_err returns: each error passed through f.
Trait implementations 2
impl<I: TryIterator, E2> Debug for MapErr<I, E2>impl<I: TryIterator, E2> TryIterator for MapErr<I, E2>
ResultsSource
pub struct Results<I>results's adapter.
Trait implementations 2
impl<I: TryIterator> Debug for Results<I>impl<I: TryIterator> Iterator for Results<I>
TryChainSource
pub struct TryChain<I, J>The iterator TryIterator::chain returns: every item of the first, then of
the second.
Trait implementations 2
impl<I: TryIterator, J: TryIterator<Item = I::Item, Error = I::Error>> Debug for TryChain<I, J>impl<I: TryIterator, J: TryIterator<Item = I::Item, Error = I::Error>> TryIterator for TryChain<I, J>
TryChunksSource
pub struct TryChunks<I>A throw drops the chunk being built.
Trait implementations 2
impl<I: TryIterator> Debug for TryChunks<I>impl<I: TryIterator> TryIterator for TryChunks<I>
TryEnumerateSource
pub struct TryEnumerate<I>The iterator TryIterator::enumerate returns: (index, item) pairs from 0.
Trait implementations 2
impl<I: TryIterator> Debug for TryEnumerate<I>impl<I: TryIterator> TryIterator for TryEnumerate<I>
TryFilterSource
pub struct TryFilter<I>The iterator TryIterator::filter returns: the items pred accepts.
Trait implementations 2
impl<I: TryIterator> Debug for TryFilter<I>impl<I: TryIterator> TryIterator for TryFilter<I>
TryFlatMapSource
pub struct TryFlatMap<I, U>flat_map: cur is the inner iterator being drained.
Trait implementations 2
impl<I: TryIterator, U: IntoIterator> Debug for TryFlatMap<I, U>impl<I: TryIterator, U: IntoIterator> TryIterator for TryFlatMap<I, U>
TryFromFnSource
pub struct TryFromFn<T, E>The TryIterator iter::try_from_fn returns: each item is f().
Trait implementations 2
impl<T, E> Debug for TryFromFn<T, E>impl<T, E> TryIterator for TryFromFn<T, E>
TryInspectSource
pub struct TryInspect<I>The iterator TryIterator::inspect returns: the items, each shown to f
first.
Trait implementations 2
impl<I: TryIterator> Debug for TryInspect<I>impl<I: TryIterator> TryIterator for TryInspect<I>
TryMapSource
pub struct TryMap<I, U>The iterator TryIterator::map returns: f of each item.
Trait implementations 2
impl<I: TryIterator, U> Debug for TryMap<I, U>impl<I: TryIterator, U> TryIterator for TryMap<I, U>
TryPeekableSource
pub struct TryPeekable<I>The iterator TryIterator::peekable returns: the items, with a look at the
next.
The next item, left in place for the following
next; a throw leaves nothing peeked.
Trait implementations 2
impl<I: TryIterator> Debug for TryPeekable<I>impl<I: TryIterator> TryIterator for TryPeekable<I>
TryScanSource
pub struct TryScan<I, S>The iterator TryIterator::scan returns: f of a running state and each
item.
Trait implementations 2
impl<I: TryIterator, S> Debug for TryScan<I, S>impl<I: TryIterator, S> TryIterator for TryScan<I, S>
TrySkipSource
pub struct TrySkip<I>The iterator TryIterator::skip returns: all but the first n items.
Trait implementations 2
impl<I: TryIterator> Debug for TrySkip<I>impl<I: TryIterator> TryIterator for TrySkip<I>
TrySkipWhileSource
pub struct TrySkipWhile<I>The iterator TryIterator::skip_while returns: items from the first pred
rejects.
Trait implementations 2
impl<I: TryIterator> Debug for TrySkipWhile<I>impl<I: TryIterator> TryIterator for TrySkipWhile<I>
TryStepBySource
pub struct TryStepBy<I>The iterator TryIterator::step_by returns: the first item, then every
stepth.
Trait implementations 2
impl<I: TryIterator> Debug for TryStepBy<I>impl<I: TryIterator> TryIterator for TryStepBy<I>
TryTakeSource
pub struct TryTake<I>The iterator TryIterator::take returns: at most the first n items.
Trait implementations 2
impl<I: TryIterator> Debug for TryTake<I>impl<I: TryIterator> TryIterator for TryTake<I>
TryTakeWhileSource
pub struct TryTakeWhile<I>The iterator TryIterator::take_while returns: items while pred accepts
them.
Trait implementations 2
impl<I: TryIterator> Debug for TryTakeWhile<I>impl<I: TryIterator> TryIterator for TryTakeWhile<I>
TryZipSource
pub struct TryZip<I, J>The iterator TryIterator::zip returns: pairs of items; ends with the
shorter side.
Trait implementations 2
impl<I: TryIterator, J: Iterator> Debug for TryZip<I, J>impl<I: TryIterator, J: Iterator> TryIterator for TryZip<I, J>
GenIterSource
pub struct GenIter<T>TryGenIterSource
pub struct TryGenIter<T, E>A throwing generator (yield fn f() throws (E) -> T): a
TryIterator whose next throws what the body throws,
stepped with try for x in g { .. }.
Trait implementations 2
impl<T, E> Debug for TryGenIter<T, E>impl<T, E> TryIterator for TryGenIter<T, E>
Traits
TryIteratorSource
pub trait TryIteratorAn iterator whose next can fail: an item, none at the end, or a
thrown Error. What a next after a throw does is the type's
documented choice: it may go on (fs::Walk) or end (fs::ReadDir).
try for x in it { .. } steps it: the try covers the iterable and
each next, not the body. The adapters' functions may throw Error
(a non-throwing function fits too); map_err changes it.
The next item, or
noneonce the iterator is done.Apply
fto each item.Keep the items
predaccepts.The same items, each error passed through
f: the only way to changeError(.map_err(|e| -> any Error { e })erases it).
Sourceconst fn enumerate(§self, start: usize = 0usize) -> TryEnumerate<Self> (index, item)pairs, the index counting fromstart.
Sourceconst fn zip<J: IntoIterator>(§self, other: J) -> TryZip<Self, J::IntoIter> Pairs of this iterator's items and a plain iterator's, until either ends.
Sourceconst fn chain<J: TryIterator<Item = Item, Error = Error>>(§self, other: J) -> TryChain<Self, J> This iterator's items, then
other's.At most the first
nitems.All but the first
nitems.Every
stepth item, starting with the first (step> 0).
Sourceconst fn take_while(§self, pred: fn(Item) throws (Error) -> bool) -> TryTakeWhile<Self> The items before the first one
predrejects.
Sourceconst fn skip_while(§self, pred: fn(Item) throws (Error) -> bool) -> TrySkipWhile<Self> The items from the first one
predrejects on.
Sourceconst fn flat_map<U: IntoIterator>(§self, f: fn(Item) throws (Error) -> U) -> TryFlatMap<Self, U> The items of each
f(item)(a plainIntoIterator).The running state:
f(init, x0), thenfof that andx1, ...
Sourceconst fn inspect(§self, f: fn(Item) throws (Error)) -> TryInspect<Self> The same items, each passed to
ffirst.
Sourceconst fn peekable(§self) -> TryPeekable<Self> An iterator that can look at its next item without taking it.
Lists of
sizeitems in order, the last one shorter (size> 0).A plain iterator of
Results: each error becomes aResult::Erritem and stepping goes on, so a source that ends after an error gives thatErrlast.
Sourceconst fn collect<C: FromIterator<Item>>(§self) throws (Error) -> C Gather the items into a
FromIteratorcollection; the first error stops it and is thrown.The number of items left, consuming them.
The last item.
The
nth remaining item (0-based), consuming the ones before it.Every item folded into
initbyf, in order.Calls
fon every item, in order.
Sourceconst fn reduce(§self, f: fn(Item, Item) throws (Error) -> Item) throws (Error) -> Item | none The items combined left to right with
f;nonefor none.Whether
predaccepts some item; stops at the first.Whether
predaccepts every item; stops at the first refusal.The first item
predaccepts.
Sourceconst fn position(§self, pred: fn(Item) throws (Error) -> bool) throws (Error) -> usize | none The index of the first item
predaccepts.
Sourceconst fn partition(§self, pred: fn(Item) throws (Error) -> bool) throws (Error) -> (List<Item>, List<Item>) The items
predaccepts, then the rest, each in order.The items added up;
0for none.The items multiplied;
1for none.The smallest item (the first of equals).
The largest item (the last of equals).
Sourceconst fn min_by_key<K: Ord>(§self, key: fn(Item) throws (Error) -> K) throws (Error) -> Item | none The item with the smallest
key(the first of equals).
Sourceconst fn max_by_key<K: Ord>(§self, key: fn(Item) throws (Error) -> K) throws (Error) -> Item | none The item with the largest
key(the last of equals).
IteratorSource
pub trait IteratorA source of items, one per next call; none ends it. The adapters
below are lazy: nothing runs until the iterator is consumed.
The next item, or
noneonce the iterator is done.Apply
fto each item.Keep the items
predaccepts.(index, item)pairs, the index counting fromstart.
Sourceconst fn zip<J: IntoIterator>(§self, other: J) -> Zip<Self, J::IntoIter> Pairs of this iterator's and
other's items, until either ends.
Sourceconst fn chain<J: IntoIterator<Item = Item>>(§self, other: J) -> Chain<Self, J::IntoIter> This iterator's items, then
other's (with the same item type).At most the first
nitems.All but the first
nitems.Every
stepth item, starting with the first. Panics whenstepis 0.The items back to front (
DoubleEndedIteratoronly).
Sourceconst fn flatten(§self) -> Flatten<Self> where Item: IntoIterator The items of each item (each item is
IntoIterator).
Sourceconst fn flat_map<U: IntoIterator>(§self, f: fn(Item) -> U) -> FlatMap<Self, U> The items of each
f(item)(anIntoIterator).An iterator that can look at its next item without taking it.
The items before the first one
predrejects; that one is used up and nothing after it is taken.The items from the first one
predrejects on.The running state:
f(init, x0), thenfof that andx1, and so on (foldthat yields each step).
Sourceconst fn inspect(§self, f: fn(Item)) -> InspectIter<Self> The same items, each passed to
ffirst (for side effects).The items over and over, restarting from a copy of this iterator; empty if it is empty.
Lists of
sizeitems in order, the last one shorter when the items run out (size> 0).Lists of
sizeconsecutive items, sliding by one (size> 0); nothing when there are fewer thansizeitems.The number of items left when that is known without stepping (no item is computed), else
none.countuses it.The number of items left:
exact_lenwhen known, else stepped.The items added up;
0for none.f32/f64items are summed pairwise: blocks of 128 items, each added left to right, and the block sums combined as a binary counter merges equal-sized partial sums, the rest added last to first. The order depends only on the item count, so the result is the same on every target.The items multiplied;
1for none.Every item folded into
initbyf, in order.foldwith a throwingf: the first error stops the loop and is thrown.for_eachwith a throwingf: the first error stops the loop and is thrown.The items
predaccepts, then the rest, each in order.The pairs split into a list of firsts and a list of seconds.
The items combined left to right with
f;nonefor none.Whether
predaccepts some item; stops at the first.Whether
predaccepts every item; stops at the first refusal.The first item
predaccepts.The index of the first item
predaccepts.
Sourceconst fn min(§self) -> Item | none where Item: PartialOrd The smallest item (the first of equals). An item unordered with itself (a float NaN) is the result: NaN propagates, as IEEE 754
minimumand NumPymin.
Sourceconst fn max(§self) -> Item | none where Item: PartialOrd The largest item (the last of equals). An item unordered with itself (a float NaN) is the result: NaN propagates, as IEEE 754
maximumand NumPymax.The item with the smallest
key(the first of equals).The item with the largest
key(the last of equals).The last item.
The
nth remaining item (0-based), consuming the ones before it.Calls
fon every item, in order.
Sourceconst fn collect<C: FromIterator<Item> = List<Item>>(§self) -> C Gather the items into a
FromIteratorcollection: the slot's type picks it (let s: Set<i32> = it.collect();), aListwhen nothing does.The items in a new
List.The items in a new
Set(first occurrences, in order).The
(key, value)items in a newDict; a later key's value replaces an earlier one's.The items in a new sorted
List, largest first whenreverse. The sort is stable. Python:sorted(xs, reverse=...).
Sourceconst fn sorted_by_key<K: Ord>(§self, key: fn(Item) -> K, reverse: bool = false) -> List<Item> The items in a new
Listsorted bykey, largest key first whenreverse; equal keys keep their order. Python:sorted(xs, key=f, reverse=...).The items in a new
Listsorted bycmp; equal items keep their order.How often each item occurs, keyed in first-seen order. Python:
collections.Counter(xs);most_common(n)on the result ranks them.The items grouped by
key: each key, in first-seen order, with its items in order. Unlike Python'sitertools.groupby, equal keys need not be adjacent.Each item paired with the next:
(x0, x1), (x1, x2), ..; nothing for fewer than two items.
Sourceconst fn combinations(§self, k: usize) -> Combinations<Item> Every choice of
kitems in item order, as lists, in lexicographic order of positions. The items are gathered first. Python:itertools.combinations(xs, k).
Sourceconst fn permutations(§self, k: usize | none = none) -> Permutations<Item> Every ordering of
kdistinct-position items (all items whenkisnone), as lists, in lexicographic order of positions. The items are gathered first. Python:itertools.permutations(xs, k).
DoubleEndedIteratorSource
pub trait DoubleEndedIterator: IteratorAn iterator that can also step from the back.
The next item from the back, or
nonewhen the two ends meet.foldfrom the back.The last item
predaccepts, searching from the back.
ExactSizeIteratorSource
pub trait ExactSizeIterator: IteratorAn iterator that knows how many items remain.
The number of items left.
IntoIteratorSource
pub trait IntoIteratorA value for can walk: into_iter turns it into an
iterator.
An iterator over the value.
FromIteratorSource
pub trait FromIterator<A>A collection buildable from an iterator (collect).
Sourcefn from_iter<I: IntoIterator<Item = A>>(§it: I) -> Self A collection holding the items of
it, in iteration order.
Functions
from_resultsSource
pub const fn from_results<T, E, I: IntoIterator<Item = Result<T, E>>>(it: I) -> FromResults<I::IntoIter, T, E> A plain iterator of Results as a TryIterator: an Err item is
thrown, then stepping goes on.
infallibleSource
pub const fn infallible<E, I: IntoIterator>(it: I) -> Infallible<I::IntoIter, E> A plain iterator as a TryIterator that never throws, for an API
that takes one: iter::infallible::<IoError>(names).
try_from_fnSource
pub const fn try_from_fn<T, E>(f: fn() throws (E) -> (T | none)) -> TryFromFn<T, E> A TryIterator whose next calls f.