Module std::iter [] [src]

Composable external iterators

The Iterator`Iterator` trait

This module defines Rust's core iteration trait. The Iterator`Iteratortrait has one unimplemented method,` trait has one unimplemented method, next`next. All other methods are derived through default methods to perform operations such as`. All other methods are derived through default methods to perform operations such as zip`zip,`, chain`chain,`, enumerate`enumerate, and`, and fold`fold`.

The goal of this module is to unify iteration across all containers in Rust. An iterator can be considered as a state machine which is used to track which element will be yielded next.

There are various extensions also defined in this module to assist with various types of iteration, such as the DoubleEndedIterator`DoubleEndedIteratorfor iterating in reverse, the` for iterating in reverse, the FromIterator`FromIterator` trait for creating a container from an iterator, and much more.

Rust's for`for` loop

The special syntax used by rust's for`forloop is based around the` loop is based around the IntoIterator`IntoIteratortrait defined in this module.` trait defined in this module. for`forloops can be viewed as a syntactical expansion into a` loops can be viewed as a syntactical expansion into a loop`loop, for example, the`, for example, the for`forloop in this example is essentially translated to the` loop in this example is essentially translated to the loop`loop` below.

fn main() { let values = vec![1, 2, 3]; for x in values { println!("{}", x); } // Rough translation of the iteration without a `for` iterator. let values = vec![1, 2, 3]; let mut it = values.into_iter(); loop { match it.next() { Some(x) => println!("{}", x), None => break, } } }
let values = vec![1, 2, 3];

for x in values {
    println!("{}", x);
}

// Rough translation of the iteration without a `for` iterator.
let mut it = values.into_iter();
loop {
    match it.next() {
        Some(x) => println!("{}", x),
        None => break,
    }
}

Because Iterator`Iterators implement`s implement IntoIterator`IntoIterator, this`, this for`for` loop syntax can be applied to any iterator over any type.

Modules

order [Unstable]

Functions for lexicographical ordering of sequences.

Structs

Chain

An iterator that strings two iterators together

Cloned

An iterator that clones the elements of an underlying iterator

Cycle

An iterator that repeats endlessly

Enumerate

An iterator that yields the current count and the element during iteration

Filter

An iterator that filters the elements of iter`iterwith` with predicate`predicate`

FilterMap

An iterator that uses f`fto both filter and map elements from` to both filter and map elements from iter`iter`

FlatMap

An iterator that maps each element to an iterator, and yields the elements of the produced iterators

Fuse

An iterator that yields None`Noneforever after the underlying iterator yields` forever after the underlying iterator yields None`None` once.

Inspect

An iterator that calls a function with a reference to each element before yielding it.

Map

An iterator that maps the values of iter`iterwith` with f`f`

Peekable

An iterator with a peek()`peek()` that returns an optional reference to the next element.

Repeat

An iterator that repeats an element endlessly

Rev

An double-ended iterator with the direction inverted

Scan

An iterator to maintain state while iterating another iterator

Skip

An iterator that skips over n`nelements of` elements of iter`iter`.

SkipWhile

An iterator that rejects elements while predicate`predicate` is true

Take

An iterator that only iterates over the first n`niterations of` iterations of iter`iter`.

TakeWhile

An iterator that only accepts elements while predicate`predicate` is true

Zip

An iterator that iterates two other iterators simultaneously

RangeInclusive [Unstable]

An iterator over the range [start, stop]

StepBy [Unstable]

An adapter for stepping range iterators by a custom amount.

Unfold [Unstable]

An iterator that passes mutable state to a closure and yields the result.

Enums

MinMaxResult [Unstable]

MinMaxResult`MinMaxResultis an enum returned by` is an enum returned by min_max`min_max. See`. See Iterator::min_max`Iterator::min_max` for more detail.

Traits

DoubleEndedIterator

A range iterator able to yield elements from both ends

ExactSizeIterator

An iterator that knows its exact length

Extend

A type growable from an Iterator`Iterator` implementation

FromIterator

Conversion from an Iterator`Iterator`

IntoIterator

Conversion into an Iterator`Iterator`

Iterator

An interface for dealing with "external iterators". These types of iterators can be resumed at any time as all state is stored internally as opposed to being located on the call stack.

RandomAccessIterator [Unstable]

An object implementing random access indexing by usize`usize`

Step [Unstable]

Objects that can be stepped over in both directions.

Functions

repeat

Creates a new iterator that endlessly repeats the element elt`elt`.

iterate [Unstable]

Creates a new iterator that produces an infinite sequence of repeated applications of the given function f`f`.

range_inclusive [Unstable]

Returns an iterator over the range [start, stop].

Type Definitions

Iterate [Unstable]