pub trait Iterator<A> { fn next(&mut self) -> Option<A>; fn size_hint(&self) -> (uint, Option<uint>) { ... } fn chain<U: Iterator<A>>(self, other: U) -> Chain<Self, U> { ... } fn zip<B, U: Iterator<B>>(self, other: U) -> Zip<Self, U> { ... } fn map<'r, B>(self, f: &'r fn(A) -> B) -> Map<'r, A, B, Self> { ... } fn filter<'r>(self, predicate: &'r fn(&A) -> bool) -> Filter<'r, A, Self> { ... } fn filter_map<'r, B>(self, f: &'r fn(A) -> Option<B>) -> FilterMap<'r, A, B, Self> { ... } fn enumerate(self) -> Enumerate<Self> { ... } fn peekable(self) -> Peekable<A, Self> { ... } fn skip_while<'r>(self, predicate: &'r fn(&A) -> bool) -> SkipWhile<'r, A, Self> { ... } fn take_while<'r>(self, predicate: &'r fn(&A) -> bool) -> TakeWhile<'r, A, Self> { ... } fn skip(self, n: uint) -> Skip<Self> { ... } fn take(self, n: uint) -> Take<Self> { ... } fn scan<'r, St, B>(self, initial_state: St, f: &'r fn(&mut St, A) -> Option<B>) -> Scan<'r, A, B, Self, St> { ... } fn flat_map<'r, B, U: Iterator<B>>(self, f: &'r fn(A) -> U) -> FlatMap<'r, A, Self, U> { ... } fn fuse(self) -> Fuse<Self> { ... } fn inspect<'r>(self, f: &'r fn(&A)) -> Inspect<'r, A, Self> { ... } fn advance(&mut self, f: &fn(A) -> bool) -> bool { ... } fn collect<B: FromIterator<A>>(&mut self) -> B { ... } fn to_owned_vec(&mut self) -> ~[A] { ... } fn nth(&mut self, n: uint) -> Option<A> { ... } fn last(&mut self) -> Option<A> { ... } fn fold<B>(&mut self, init: B, f: &fn(B, A) -> B) -> B { ... } fn len(&mut self) -> uint { ... } fn all(&mut self, f: &fn(A) -> bool) -> bool { ... } fn any(&mut self, f: &fn(A) -> bool) -> bool { ... } fn find(&mut self, predicate: &fn(&A) -> bool) -> Option<A> { ... } fn position(&mut self, predicate: &fn(A) -> bool) -> Option<uint> { ... } fn count(&mut self, predicate: &fn(A) -> bool) -> uint { ... } fn max_by<B: Ord>(&mut self, f: &fn(&A) -> B) -> Option<A> { ... } fn min_by<B: Ord>(&mut self, f: &fn(&A) -> B) -> Option<A> { ... } }
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.
The Iterator protocol states that an iterator yields a (potentially-empty,
potentially-infinite) sequence of values, and returns None
to signal that
it's finished. The Iterator protocol does not define behavior after None
is returned. A concrete Iterator implementation may choose to behave however
it wishes, either by returning None
infinitely, or by doing something
else.
fn next(&mut self) -> Option<A>
Advance the iterator and return the next value. Return None
when the end is reached.
fn size_hint(&self) -> (uint, Option<uint>)
Return a lower bound and upper bound on the remaining length of the iterator.
The common use case for the estimate is pre-allocating space to store the results.
fn chain<U: Iterator<A>>(self, other: U) -> Chain<Self, U>
Chain this iterator with another, returning a new iterator which will finish iterating over the current iterator, and then it will iterate over the other specified iterator.
let a = [0];
let b = [1];
let mut it = a.iter().chain(b.iter());
assert_eq!(it.next().get(), &0);
assert_eq!(it.next().get(), &1);
assert!(it.next().is_none());
fn zip<B, U: Iterator<B>>(self, other: U) -> Zip<Self, U>
Creates an iterator which iterates over both this and the specified iterators simultaneously, yielding the two elements as pairs. When either iterator returns None, all further invocations of next() will return None.
let a = [0];
let b = [1];
let mut it = a.iter().zip(b.iter());
assert_eq!(it.next().get(), (&0, &1));
assert!(it.next().is_none());
fn map<'r, B>(self, f: &'r fn(A) -> B) -> Map<'r, A, B, Self>
Creates a new iterator which will apply the specified function to each element returned by the first, yielding the mapped element instead.
let a = [1, 2];
let mut it = a.iter().map(|&x| 2 * x);
assert_eq!(it.next().get(), 2);
assert_eq!(it.next().get(), 4);
assert!(it.next().is_none());
fn filter<'r>(self, predicate: &'r fn(&A) -> bool) -> Filter<'r, A, Self>
Creates an iterator which applies the predicate to each element returned
by this iterator. Only elements which have the predicate evaluate to
true
will be yielded.
let a = [1, 2];
let mut it = a.iter().filter(|&x| *x > 1);
assert_eq!(it.next().get(), &2);
assert!(it.next().is_none());
fn filter_map<'r, B>(self, f: &'r fn(A) -> Option<B>) -> FilterMap<'r, A, B, Self>
Creates an iterator which both filters and maps elements. If the specified function returns None, the element is skipped. Otherwise the option is unwrapped and the new value is yielded.
let a = [1, 2];
let mut it = a.iter().filter_map(|&x| if x > 1 {Some(2 * x)} else {None});
assert_eq!(it.next().get(), 4);
assert!(it.next().is_none());
fn enumerate(self) -> Enumerate<Self>
Creates an iterator which yields a pair of the value returned by this iterator plus the current index of iteration.
let a = [100, 200];
let mut it = a.iter().enumerate();
assert_eq!(it.next().get(), (0, &100));
assert_eq!(it.next().get(), (1, &200));
assert!(it.next().is_none());
fn peekable(self) -> Peekable<A, Self>
Creates an iterator that has a .peek()
method
that returns a optional reference to the next element.
let a = [100, 200, 300];
let mut it = xs.iter().map(|&x|x).peekable();
assert_eq!(it.peek().unwrap(), &100);
assert_eq!(it.next().unwrap(), 100);
assert_eq!(it.next().unwrap(), 200);
assert_eq!(it.peek().unwrap(), &300);
assert_eq!(it.peek().unwrap(), &300);
assert_eq!(it.next().unwrap(), 300);
assert!(it.peek().is_none());
assert!(it.next().is_none());
fn skip_while<'r>(self, predicate: &'r fn(&A) -> bool) -> SkipWhile<'r, A, Self>
Creates an iterator which invokes the predicate on elements until it returns false. Once the predicate returns false, all further elements are yielded.
let a = [1, 2, 3, 2, 1];
let mut it = a.iter().skip_while(|&a| *a < 3);
assert_eq!(it.next().get(), &3);
assert_eq!(it.next().get(), &2);
assert_eq!(it.next().get(), &1);
assert!(it.next().is_none());
fn take_while<'r>(self, predicate: &'r fn(&A) -> bool) -> TakeWhile<'r, A, Self>
Creates an iterator which yields elements so long as the predicate returns true. After the predicate returns false for the first time, no further elements will be yielded.
let a = [1, 2, 3, 2, 1];
let mut it = a.iter().take_while(|&a| *a < 3);
assert_eq!(it.next().get(), &1);
assert_eq!(it.next().get(), &2);
assert!(it.next().is_none());
fn skip(self, n: uint) -> Skip<Self>
Creates an iterator which skips the first n
elements of this iterator,
and then it yields all further items.
let a = [1, 2, 3, 4, 5];
let mut it = a.iter().skip(3);
assert_eq!(it.next().get(), &4);
assert_eq!(it.next().get(), &5);
assert!(it.next().is_none());
fn take(self, n: uint) -> Take<Self>
Creates an iterator which yields the first n
elements of this
iterator, and then it will always return None.
let a = [1, 2, 3, 4, 5];
let mut it = a.iter().take(3);
assert_eq!(it.next().get(), &1);
assert_eq!(it.next().get(), &2);
assert_eq!(it.next().get(), &3);
assert!(it.next().is_none());
fn scan<'r, St, B>(self, initial_state: St, f: &'r fn(&mut St, A) -> Option<B>) -> Scan<'r, A, B, Self, St>
Creates a new iterator which behaves in a similar fashion to foldl. There is a state which is passed between each iteration and can be mutated as necessary. The yielded values from the closure are yielded from the Scan instance when not None.
let a = [1, 2, 3, 4, 5];
let mut it = a.iter().scan(1, |fac, &x| {
*fac = *fac * x;
Some(*fac)
});
assert_eq!(it.next().get(), 1);
assert_eq!(it.next().get(), 2);
assert_eq!(it.next().get(), 6);
assert_eq!(it.next().get(), 24);
assert_eq!(it.next().get(), 120);
assert!(it.next().is_none());
fn flat_map<'r, B, U: Iterator<B>>(self, f: &'r fn(A) -> U) -> FlatMap<'r, A, Self, U>
Creates an iterator that maps each element to an iterator, and yields the elements of the produced iterators
let xs = [2u, 3];
let ys = [0u, 1, 0, 1, 2];
let mut it = xs.iter().flat_map(|&x| count(0u, 1).take(x));
// Check that `it` has the same elements as `ys`
let mut i = 0;
for x: uint in it {
assert_eq!(x, ys[i]);
i += 1;
}
fn fuse(self) -> Fuse<Self>
Creates an iterator that yields None
forever after the underlying
iterator yields None
. Random-access iterator behavior is not
affected, only single and double-ended iterator behavior.
fn process<U: Iterator<int>>(it: U) -> int {
let mut it = it.fuse();
let mut sum = 0;
for x in it {
if x > 5 {
break;
}
sum += x;
}
// did we exhaust the iterator?
if it.next().is_none() {
sum += 1000;
}
sum
}
let x = ~[1,2,3,7,8,9];
assert_eq!(process(x.move_iter()), 1006);
fn inspect<'r>(self, f: &'r fn(&A)) -> Inspect<'r, A, Self>
Creates an iterator that calls a function with a reference to each element before yielding it. This is often useful for debugging an iterator pipeline.
# Example
let xs = [1u, 4, 2, 3, 8, 9, 6];
let sum = xs.iter()
.map(|&x| x)
.inspect(|&x| debug!("filtering %u", x))
.filter(|&x| x % 2 == 0)
.inspect(|&x| debug!("%u made it through", x))
.sum();
println(sum.to_str());
fn advance(&mut self, f: &fn(A) -> bool) -> bool
An adaptation of an external iterator to the for-loop protocol of rust.
use std::iter::count;
for i in count(0, 10) {
printfln!("%d", i);
}
fn collect<B: FromIterator<A>>(&mut self) -> B
Loops through the entire iterator, collecting all of the elements into
a container implementing FromIterator
.
let a = [1, 2, 3, 4, 5];
let b: ~[int] = a.iter().map(|&x| x).collect();
assert!(a == b);
fn to_owned_vec(&mut self) -> ~[A]
Loops through the entire iterator, collecting all of the elements into a unique vector. This is simply collect() specialized for vectors.
let a = [1, 2, 3, 4, 5];
let b: ~[int] = a.iter().map(|&x| x).to_owned_vec();
assert!(a == b);
fn nth(&mut self, n: uint) -> Option<A>
Loops through n
iterations, returning the n
th element of the
iterator.
let a = [1, 2, 3, 4, 5];
let mut it = a.iter();
assert!(it.nth(2).get() == &3);
assert!(it.nth(2) == None);
fn last(&mut self) -> Option<A>
Loops through the entire iterator, returning the last element of the iterator.
let a = [1, 2, 3, 4, 5];
assert!(a.iter().last().get() == &5);
fn fold<B>(&mut self, init: B, f: &fn(B, A) -> B) -> B
Performs a fold operation over the entire iterator, returning the eventual state at the end of the iteration.
let a = [1, 2, 3, 4, 5];
assert!(a.iter().fold(0, |a, &b| a + b) == 15);
fn len(&mut self) -> uint
Counts the number of elements in this iterator.
let a = [1, 2, 3, 4, 5];
let mut it = a.iter();
assert!(it.len() == 5);
assert!(it.len() == 0);
fn all(&mut self, f: &fn(A) -> bool) -> bool
Tests whether the predicate holds true for all elements in the iterator.
let a = [1, 2, 3, 4, 5];
assert!(a.iter().all(|&x| *x > 0));
assert!(!a.iter().all(|&x| *x > 2));
fn any(&mut self, f: &fn(A) -> bool) -> bool
Tests whether any element of an iterator satisfies the specified predicate.
let a = [1, 2, 3, 4, 5];
let mut it = a.iter();
assert!(it.any(|&x| *x == 3));
assert!(!it.any(|&x| *x == 3));
fn find(&mut self, predicate: &fn(&A) -> bool) -> Option<A>
Return the first element satisfying the specified predicate
fn position(&mut self, predicate: &fn(A) -> bool) -> Option<uint>
Return the index of the first element satisfying the specified predicate
fn count(&mut self, predicate: &fn(A) -> bool) -> uint
Count the number of elements satisfying the specified predicate
fn max_by<B: Ord>(&mut self, f: &fn(&A) -> B) -> Option<A>
Return the element that gives the maximum value from the specified function.
let xs = [-3, 0, 1, 5, -10];
assert_eq!(*xs.iter().max_by(|x| x.abs()).unwrap(), -10);
fn min_by<B: Ord>(&mut self, f: &fn(&A) -> B) -> Option<A>
Return the element that gives the minimum value from the specified function.
let xs = [-3, 0, 1, 5, -10];
assert_eq!(*xs.iter().min_by(|x| x.abs()).unwrap(), 0);
IncomingIterator<'self, A>
ByteIterator<R>
Parser<'self>
CStringIterator<'self>
TrieSetIterator<'self>
TrieMapIterator<'self, T>
HashSetMoveIterator<K>
HashSetIterator<'self, K>
HashMapMoveIterator<K, V>
HashMapMutIterator<'self, K, V>
HashMapIterator<'self, K, V>
OptionIterator<A>
Repeat<A>
RangeStepInclusive<A>
RangeStep<A>
RangeInclusive<A>
Range<A>
Counter<A>
Unfold<'self, A, St>
Inspect<'self, A, T>
Fuse<T>
FlatMap<'self, A, T, U>
Scan<'self, A, B, T, St>
Take<T>
Skip<T>
TakeWhile<'self, A, T>
SkipWhile<'self, A, T>
Peekable<A, T>
Enumerate<T>
FilterMap<'self, A, B, T>
Filter<'self, A, T>
Map<'self, A, B, T>
Zip<T, U>
Chain<T, U>
Cycle<T>
Invert<T>
NormalizationIterator<'self>
StrSplitIterator<'self>
MatchesIndexIterator<'self>
CharSplitNIterator<'self, Sep>
CharSplitIterator<'self, Sep>
CharOffsetIterator<'self>
CharIterator<'self>
MoveRevIterator<T>
MoveIterator<T>
VecMutIterator<'self, T>
VecIterator<'self, T>
ChunkIter<'self, T>
WindowIter<'self, T>
Permutations<T>
ElementSwaps
RSplitIterator<'self, T>
SplitIterator<'self, T>
Prefix searches with a type followed by a colon (e.g.
fn:
) to restrict the search to a given type.
Accepted types are: fn
, mod
,
struct
(or str
), enum
,
trait
, typedef
(or
tdef
).