Auto merge of #21382 - tshepang:improve-iter-docs, r=alexcrichton

This commit is contained in:
bors
2015-01-23 09:26:34 +00:00
+263 -236
View File
@@ -32,7 +32,7 @@
//! into a `loop`, for example, the `for` loop in this example is essentially
//! translated to the `loop` below.
//!
//! ```rust
//! ```
//! let values = vec![1i, 2, 3];
//!
//! // "Syntactical sugar" taking advantage of an iterator
@@ -68,7 +68,7 @@
use option::Option;
use option::Option::{Some, None};
use std::marker::Sized;
use uint;
use usize;
/// 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
@@ -93,10 +93,10 @@ pub trait Iterator {
/// Returns a lower and upper bound on the remaining length of the iterator.
///
/// An upper bound of `None` means either there is no known upper bound, or the upper bound
/// does not fit within a `uint`.
/// does not fit within a `usize`.
#[inline]
#[stable]
fn size_hint(&self) -> (uint, Option<uint>) { (0, None) }
fn size_hint(&self) -> (usize, Option<usize>) { (0, None) }
}
// FIXME(#21363) remove `old_impl_check` when bug is fixed
@@ -135,26 +135,25 @@ pub trait Extend<A> {
pub trait IteratorExt: Iterator + Sized {
/// Counts the number of elements in this iterator.
///
/// # Example
/// # Examples
///
/// ```rust
/// let a = [1i, 2, 3, 4, 5];
/// let mut it = a.iter();
/// assert!(it.count() == 5);
/// ```
/// let a = [1, 2, 3, 4, 5];
/// assert_eq!(a.iter().count(), 5);
/// ```
#[inline]
#[stable]
fn count(self) -> uint {
fn count(self) -> usize {
self.fold(0, |cnt, _x| cnt + 1)
}
/// Loops through the entire iterator, returning the last element of the
/// iterator.
///
/// # Example
/// # Examples
///
/// ```rust
/// let a = [1i, 2, 3, 4, 5];
/// ```
/// let a = [1, 2, 3, 4, 5];
/// assert!(a.iter().last().unwrap() == &5);
/// ```
#[inline]
@@ -168,17 +167,17 @@ fn last(mut self) -> Option<Self::Item> {
/// Loops through `n` iterations, returning the `n`th element of the
/// iterator.
///
/// # Example
/// # Examples
///
/// ```rust
/// let a = [1i, 2, 3, 4, 5];
/// ```
/// let a = [1, 2, 3, 4, 5];
/// let mut it = a.iter();
/// assert!(it.nth(2).unwrap() == &3);
/// assert!(it.nth(2) == None);
/// ```
#[inline]
#[stable]
fn nth(&mut self, mut n: uint) -> Option<Self::Item> {
fn nth(&mut self, mut n: usize) -> Option<Self::Item> {
for x in *self {
if n == 0 { return Some(x) }
n -= 1;
@@ -190,11 +189,11 @@ fn nth(&mut self, mut n: uint) -> Option<Self::Item> {
/// finish iterating over the current iterator, and then iterate
/// over the other specified iterator.
///
/// # Example
/// # Examples
///
/// ```rust
/// let a = [0i];
/// let b = [1i];
/// ```
/// let a = [0];
/// let b = [1];
/// let mut it = a.iter().chain(b.iter());
/// assert_eq!(it.next().unwrap(), &0);
/// assert_eq!(it.next().unwrap(), &1);
@@ -213,14 +212,13 @@ fn chain<U>(self, other: U) -> Chain<Self, U> where
/// either iterator returns None, all further invocations of next() will
/// return None.
///
/// # Example
/// # Examples
///
/// ```rust
/// let a = [0i];
/// let b = [1i];
/// ```
/// let a = [0];
/// let b = [1];
/// let mut it = a.iter().zip(b.iter());
/// let (x0, x1) = (0i, 1i);
/// assert_eq!(it.next().unwrap(), (&x0, &x1));
/// assert_eq!(it.next().unwrap(), (&0, &1));
/// assert!(it.next().is_none());
/// ```
#[inline]
@@ -234,10 +232,10 @@ fn zip<B, U>(self, other: U) -> Zip<Self, U> where
/// Creates a new iterator that will apply the specified function to each
/// element returned by the first, yielding the mapped element instead.
///
/// # Example
/// # Examples
///
/// ```rust
/// let a = [1i, 2];
/// ```
/// let a = [1, 2];
/// let mut it = a.iter().map(|&x| 2 * x);
/// assert_eq!(it.next().unwrap(), 2);
/// assert_eq!(it.next().unwrap(), 4);
@@ -252,13 +250,13 @@ fn map<B, F>(self, f: F) -> Map<Self::Item, B, Self, F> where
}
/// Creates an iterator that applies the predicate to each element returned
/// by this iterator. Only elements that have the predicate evaluate to
/// `true` will be yielded.
/// by this iterator. The only elements that will be yieled are those that
/// make the predicate evaluate to `true`.
///
/// # Example
/// # Examples
///
/// ```rust
/// let a = [1i, 2];
/// ```
/// let a = [1, 2];
/// let mut it = a.iter().filter(|&x| *x > 1);
/// assert_eq!(it.next().unwrap(), &2);
/// assert!(it.next().is_none());
@@ -275,10 +273,10 @@ fn filter<P>(self, predicate: P) -> Filter<Self::Item, Self, P> where
/// If the specified function returns None, the element is skipped.
/// Otherwise the option is unwrapped and the new value is yielded.
///
/// # Example
/// # Examples
///
/// ```rust
/// let a = [1i, 2];
/// ```
/// let a = [1, 2];
/// let mut it = a.iter().filter_map(|&x| if x > 1 {Some(2 * x)} else {None});
/// assert_eq!(it.next().unwrap(), 4);
/// assert!(it.next().is_none());
@@ -294,14 +292,13 @@ fn filter_map<B, F>(self, f: F) -> FilterMap<Self::Item, B, Self, F> where
/// Creates an iterator that yields a pair of the value returned by this
/// iterator plus the current index of iteration.
///
/// # Example
/// # Examples
///
/// ```rust
/// let a = [100i, 200];
/// ```
/// let a = [100, 200];
/// let mut it = a.iter().enumerate();
/// let (x100, x200) = (100i, 200i);
/// assert_eq!(it.next().unwrap(), (0, &x100));
/// assert_eq!(it.next().unwrap(), (1, &x200));
/// assert_eq!(it.next().unwrap(), (0, &100));
/// assert_eq!(it.next().unwrap(), (1, &200));
/// assert!(it.next().is_none());
/// ```
#[inline]
@@ -313,10 +310,10 @@ fn enumerate(self) -> Enumerate<Self> {
/// Creates an iterator that has a `.peek()` method
/// that returns an optional reference to the next element.
///
/// # Example
/// # Examples
///
/// ```rust
/// let xs = [100i, 200, 300];
/// ```
/// let xs = [100, 200, 300];
/// let mut it = xs.iter().map(|x| *x).peekable();
/// assert_eq!(*it.peek().unwrap(), 100);
/// assert_eq!(it.next().unwrap(), 100);
@@ -337,14 +334,14 @@ fn peekable(self) -> Peekable<Self::Item, Self> {
/// until it returns false. Once the predicate returns false, that
/// element and all further elements are yielded.
///
/// # Example
/// # Examples
///
/// ```rust
/// let a = [1i, 2, 3, 2, 1];
/// ```
/// let a = [1, 2, 3, 4, 5];
/// let mut it = a.iter().skip_while(|&a| *a < 3);
/// assert_eq!(it.next().unwrap(), &3);
/// assert_eq!(it.next().unwrap(), &2);
/// assert_eq!(it.next().unwrap(), &1);
/// assert_eq!(it.next().unwrap(), &4);
/// assert_eq!(it.next().unwrap(), &5);
/// assert!(it.next().is_none());
/// ```
#[inline]
@@ -359,10 +356,10 @@ fn skip_while<P>(self, predicate: P) -> SkipWhile<Self::Item, Self, P> where
/// returns true. After the predicate returns false for the first time, no
/// further elements will be yielded.
///
/// # Example
/// # Examples
///
/// ```rust
/// let a = [1i, 2, 3, 2, 1];
/// ```
/// let a = [1, 2, 3, 4, 5];
/// let mut it = a.iter().take_while(|&a| *a < 3);
/// assert_eq!(it.next().unwrap(), &1);
/// assert_eq!(it.next().unwrap(), &2);
@@ -379,10 +376,10 @@ fn take_while<P>(self, predicate: P) -> TakeWhile<Self::Item, Self, P> where
/// Creates an iterator that skips the first `n` elements of this iterator,
/// and then yields all further items.
///
/// # Example
/// # Examples
///
/// ```rust
/// let a = [1i, 2, 3, 4, 5];
/// ```
/// let a = [1, 2, 3, 4, 5];
/// let mut it = a.iter().skip(3);
/// assert_eq!(it.next().unwrap(), &4);
/// assert_eq!(it.next().unwrap(), &5);
@@ -390,17 +387,17 @@ fn take_while<P>(self, predicate: P) -> TakeWhile<Self::Item, Self, P> where
/// ```
#[inline]
#[stable]
fn skip(self, n: uint) -> Skip<Self> {
fn skip(self, n: usize) -> Skip<Self> {
Skip{iter: self, n: n}
}
/// Creates an iterator that yields the first `n` elements of this
/// iterator, and then will always return None.
/// iterator.
///
/// # Example
/// # Examples
///
/// ```rust
/// let a = [1i, 2, 3, 4, 5];
/// ```
/// let a = [1, 2, 3, 4, 5];
/// let mut it = a.iter().take(3);
/// assert_eq!(it.next().unwrap(), &1);
/// assert_eq!(it.next().unwrap(), &2);
@@ -409,7 +406,7 @@ fn skip(self, n: uint) -> Skip<Self> {
/// ```
#[inline]
#[stable]
fn take(self, n: uint) -> Take<Self> {
fn take(self, n: usize) -> Take<Self> {
Take{iter: self, n: n}
}
@@ -418,10 +415,10 @@ fn take(self, n: uint) -> Take<Self> {
/// mutated as necessary. The yielded values from the closure are yielded
/// from the Scan instance when not None.
///
/// # Example
/// # Examples
///
/// ```rust
/// let a = [1i, 2, 3, 4, 5];
/// ```
/// let a = [1, 2, 3, 4, 5];
/// let mut it = a.iter().scan(1, |fac, &x| {
/// *fac = *fac * x;
/// Some(*fac)
@@ -446,21 +443,17 @@ fn scan<St, B, F>(
}
/// Creates an iterator that maps each element to an iterator,
/// and yields the elements of the produced iterators
/// and yields the elements of the produced iterators.
///
/// # Example
/// # Examples
///
/// ```rust
/// use std::iter::count;
///
/// let xs = [2u, 3];
/// let ys = [0u, 1, 0, 1, 2];
/// let mut it = xs.iter().flat_map(|&x| count(0u, 1).take(x));
/// ```
/// let xs = [2, 3];
/// let ys = [0, 1, 0, 1, 2];
/// let it = xs.iter().flat_map(|&x| std::iter::count(0, 1).take(x));
/// // Check that `it` has the same elements as `ys`
/// let mut i = 0;
/// for x in it {
/// for (i, x) in it.enumerate() {
/// assert_eq!(x, ys[i]);
/// i += 1;
/// }
/// ```
#[inline]
@@ -476,10 +469,10 @@ fn flat_map<B, U, F>(self, f: F) -> FlatMap<Self::Item, B, Self, U, F> where
/// iterator yields `None`. Random-access iterator behavior is not
/// affected, only single and double-ended iterator behavior.
///
/// # Example
/// # Examples
///
/// ```rust
/// fn process<U: Iterator<Item=int>>(it: U) -> int {
/// ```
/// fn process<U: Iterator<Item=isize>>(it: U) -> isize {
/// let mut it = it.fuse();
/// let mut sum = 0;
/// for x in it {
@@ -494,9 +487,9 @@ fn flat_map<B, U, F>(self, f: F) -> FlatMap<Self::Item, B, Self, U, F> where
/// }
/// sum
/// }
/// let x = vec![1i,2,3,7,8,9];
/// let x = vec![1, 2, 3, 7, 8, 9];
/// assert_eq!(process(x.into_iter()), 6);
/// let x = vec![1i,2,3];
/// let x = vec![1, 2, 3];
/// assert_eq!(process(x.into_iter()), 1006);
/// ```
#[inline]
@@ -509,13 +502,13 @@ fn fuse(self) -> Fuse<Self> {
/// element before yielding it. This is often useful for debugging an
/// iterator pipeline.
///
/// # Example
/// # Examples
///
/// ```rust
/// ```
/// use std::iter::AdditiveIterator;
///
/// let xs = [1u, 4, 2, 3, 8, 9, 6];
/// let sum = xs.iter()
/// let a = [1, 4, 2, 3, 8, 9, 6];
/// let sum = a.iter()
/// .map(|&x| x)
/// .inspect(|&x| println!("filtering {}", x))
/// .filter(|&x| x % 2 == 0)
@@ -536,15 +529,14 @@ fn inspect<F>(self, f: F) -> Inspect<Self::Item, Self, F> where
/// This is useful to allow applying iterator adaptors while still
/// retaining ownership of the original iterator value.
///
/// # Example
/// # Examples
///
/// ```rust
/// let mut xs = range(0u, 10);
/// ```
/// let mut it = 0..10;
/// // sum the first five values
/// let partial_sum = xs.by_ref().take(5).fold(0, |a, b| a + b);
/// let partial_sum = it.by_ref().take(5).fold(0, |a, b| a + b);
/// assert!(partial_sum == 10);
/// // xs.next() is now `5`
/// assert!(xs.next() == Some(5));
/// assert!(it.next() == Some(5));
/// ```
#[stable]
fn by_ref<'r>(&'r mut self) -> ByRef<'r, Self> {
@@ -554,12 +546,12 @@ fn by_ref<'r>(&'r mut self) -> ByRef<'r, Self> {
/// Loops through the entire iterator, collecting all of the elements into
/// a container implementing `FromIterator`.
///
/// # Example
/// # Examples
///
/// ```rust
/// let a = [1i, 2, 3, 4, 5];
/// let b: Vec<int> = a.iter().map(|&x| x).collect();
/// assert!(a.as_slice() == b.as_slice());
/// ```
/// let a = [1, 2, 3, 4, 5];
/// let b: Vec<_> = a.iter().map(|&x| x).collect();
/// assert_eq!(a, b);
/// ```
#[inline]
#[stable]
@@ -573,8 +565,8 @@ fn collect<B: FromIterator<Self::Item>>(self) -> B {
/// do not.
///
/// ```
/// let vec = vec![1i, 2i, 3i, 4i];
/// let (even, odd): (Vec<int>, Vec<int>) = vec.into_iter().partition(|&n| n % 2 == 0);
/// let vec = vec![1, 2, 3, 4];
/// let (even, odd): (Vec<_>, Vec<_>) = vec.into_iter().partition(|&n| n % 2 == 0);
/// assert_eq!(even, vec![2, 4]);
/// assert_eq!(odd, vec![1, 3]);
/// ```
@@ -600,10 +592,10 @@ fn partition<B, F>(mut self, mut f: F) -> (B, B) where
/// Performs a fold operation over the entire iterator, returning the
/// eventual state at the end of the iteration.
///
/// # Example
/// # Examples
///
/// ```rust
/// let a = [1i, 2, 3, 4, 5];
/// ```
/// let a = [1, 2, 3, 4, 5];
/// assert!(a.iter().fold(0, |a, &b| a + b) == 15);
/// ```
#[inline]
@@ -620,9 +612,9 @@ fn fold<B, F>(mut self, init: B, mut f: F) -> B where
/// Tests whether the predicate holds true for all elements in the iterator.
///
/// # Example
/// # Examples
///
/// ```rust
/// ```
/// let a = [1i, 2, 3, 4, 5];
/// assert!(a.iter().all(|x| *x > 0));
/// assert!(!a.iter().all(|x| *x > 2));
@@ -634,16 +626,18 @@ fn all<F>(mut self, mut f: F) -> bool where F: FnMut(Self::Item) -> bool {
true
}
/// Tests whether any element of an iterator satisfies the specified
/// predicate.
/// Tests whether any element of an iterator satisfies the specified predicate.
///
/// # Example
/// Does not consume the iterator past the first found element.
///
/// ```rust
/// let a = [1i, 2, 3, 4, 5];
/// # Examples
///
/// ```
/// let a = [1, 2, 3, 4, 5];
/// let mut it = a.iter();
/// assert!(it.any(|x| *x == 3));
/// assert!(!it.any(|x| *x == 3));
/// assert_eq!(it.as_slice(), [4, 5]);
///
/// ```
#[inline]
#[stable]
@@ -655,6 +649,14 @@ fn any<F>(&mut self, mut f: F) -> bool where F: FnMut(Self::Item) -> bool {
/// Returns the first element satisfying the specified predicate.
///
/// Does not consume the iterator past the first found element.
///
/// # Examples
///
/// ```
/// let a = [1, 2, 3, 4, 5];
/// let mut it = a.iter();
/// assert_eq!(it.find(|&x| *x == 3).unwrap(), &3);
/// assert_eq!(it.as_slice(), [4, 5]);
#[inline]
#[stable]
fn find<P>(&mut self, mut predicate: P) -> Option<Self::Item> where
@@ -667,9 +669,19 @@ fn find<P>(&mut self, mut predicate: P) -> Option<Self::Item> where
}
/// Return the index of the first element satisfying the specified predicate
///
/// Does not consume the iterator past the first found element.
///
/// # Examples
///
/// ```
/// let a = [1, 2, 3, 4, 5];
/// let mut it = a.iter();
/// assert_eq!(it.position(|x| *x == 3).unwrap(), 2);
/// assert_eq!(it.as_slice(), [4, 5]);
#[inline]
#[stable]
fn position<P>(&mut self, mut predicate: P) -> Option<uint> where
fn position<P>(&mut self, mut predicate: P) -> Option<usize> where
P: FnMut(Self::Item) -> bool,
{
let mut i = 0;
@@ -685,9 +697,19 @@ fn position<P>(&mut self, mut predicate: P) -> Option<uint> where
/// Return the index of the last element satisfying the specified predicate
///
/// If no element matches, None is returned.
///
/// Does not consume the iterator *before* the first found element.
///
/// # Examples
///
/// ```
/// let a = [1, 2, 2, 4, 5];
/// let mut it = a.iter();
/// assert_eq!(it.rposition(|x| *x == 2).unwrap(), 2);
/// assert_eq!(it.as_slice(), [1, 2]);
#[inline]
#[stable]
fn rposition<P>(&mut self, mut predicate: P) -> Option<uint> where
fn rposition<P>(&mut self, mut predicate: P) -> Option<usize> where
P: FnMut(Self::Item) -> bool,
Self: ExactSizeIterator + DoubleEndedIterator
{
@@ -702,10 +724,10 @@ fn rposition<P>(&mut self, mut predicate: P) -> Option<uint> where
/// Consumes the entire iterator to return the maximum element.
///
/// # Example
/// # Examples
///
/// ```rust
/// let a = [1i, 2, 3, 4, 5];
/// ```
/// let a = [1, 2, 3, 4, 5];
/// assert!(a.iter().max().unwrap() == &5);
/// ```
#[inline]
@@ -722,10 +744,10 @@ fn max(self) -> Option<Self::Item> where Self::Item: Ord
/// Consumes the entire iterator to return the minimum element.
///
/// # Example
/// # Examples
///
/// ```rust
/// let a = [1i, 2, 3, 4, 5];
/// ```
/// let a = [1, 2, 3, 4, 5];
/// assert!(a.iter().min().unwrap() == &1);
/// ```
#[inline]
@@ -753,25 +775,22 @@ fn min(self) -> Option<Self::Item> where Self::Item: Ord
/// On an iterator of length `n`, `min_max` does `1.5 * n` comparisons,
/// and so is faster than calling `min` and `max` separately which does `2 * n` comparisons.
///
/// # Example
/// # Examples
///
/// ```rust
/// ```
/// use std::iter::MinMaxResult::{NoElements, OneElement, MinMax};
///
/// let v: [int; 0] = [];
/// assert_eq!(v.iter().min_max(), NoElements);
/// let a: [isize; 0] = [];
/// assert_eq!(a.iter().min_max(), NoElements);
///
/// let v = [1i];
/// assert!(v.iter().min_max() == OneElement(&1));
/// let a = [1];
/// assert!(a.iter().min_max() == OneElement(&1));
///
/// let v = [1i, 2, 3, 4, 5];
/// assert!(v.iter().min_max() == MinMax(&1, &5));
/// let a = [1, 2, 3, 4, 5];
/// assert!(a.iter().min_max() == MinMax(&1, &5));
///
/// let v = [1i, 2, 3, 4, 5, 6];
/// assert!(v.iter().min_max() == MinMax(&1, &6));
///
/// let v = [1i, 1, 1, 1];
/// assert!(v.iter().min_max() == MinMax(&1, &1));
/// let a = [1, 1, 1, 1];
/// assert!(a.iter().min_max() == MinMax(&1, &1));
/// ```
#[unstable = "return type may change"]
fn min_max(mut self) -> MinMaxResult<Self::Item> where Self::Item: Ord
@@ -821,13 +840,13 @@ fn min_max(mut self) -> MinMaxResult<Self::Item> where Self::Item: Ord
/// Return the element that gives the maximum value from the
/// specified function.
///
/// # Example
/// # Examples
///
/// ```rust
/// ```
/// use core::num::SignedInt;
///
/// let xs = [-3i, 0, 1, 5, -10];
/// assert_eq!(*xs.iter().max_by(|x| x.abs()).unwrap(), -10);
/// let a = [-3, 0, 1, 5, -10];
/// assert_eq!(*a.iter().max_by(|x| x.abs()).unwrap(), -10);
/// ```
#[inline]
#[unstable = "may want to produce an Ordering directly; see #15311"]
@@ -850,13 +869,13 @@ fn max_by<B: Ord, F>(self, mut f: F) -> Option<Self::Item> where
/// Return the element that gives the minimum value from the
/// specified function.
///
/// # Example
/// # Examples
///
/// ```rust
/// ```
/// use core::num::SignedInt;
///
/// let xs = [-3i, 0, 1, 5, -10];
/// assert_eq!(*xs.iter().min_by(|x| x.abs()).unwrap(), 0);
/// let a = [-3, 0, 1, 5, -10];
/// assert_eq!(*a.iter().min_by(|x| x.abs()).unwrap(), 0);
/// ```
#[inline]
#[unstable = "may want to produce an Ordering directly; see #15311"]
@@ -887,7 +906,7 @@ fn min_by<B: Ord, F>(self, mut f: F) -> Option<Self::Item> where
/// of the original iterator.
///
/// Note: Random access with flipped indices still only applies to the first
/// `uint::MAX` elements of the original iterator.
/// `std::usize::MAX` elements of the original iterator.
#[inline]
#[stable]
fn rev(self) -> Rev<Self> {
@@ -898,18 +917,27 @@ fn rev(self) -> Rev<Self> {
///
/// Loops through the entire iterator, collecting the first component of
/// each item into one new container, and the second component into another.
///
/// # Examples
///
/// ```
/// let a = [(1, 2), (3, 4)];
/// let (left, right): (Vec<_>, Vec<_>) = a.iter().map(|&x| x).unzip();
/// assert_eq!([1, 3], left);
/// assert_eq!([2, 4], right);
/// ```
#[unstable = "recent addition"]
fn unzip<A, B, FromA, FromB>(mut self) -> (FromA, FromB) where
FromA: Default + Extend<A>,
FromB: Default + Extend<B>,
Self: Iterator<Item=(A, B)>,
{
struct SizeHint<A>(uint, Option<uint>);
struct SizeHint<A>(usize, Option<usize>);
impl<A> Iterator for SizeHint<A> {
type Item = A;
fn next(&mut self) -> Option<A> { None }
fn size_hint(&self) -> (uint, Option<uint>) {
fn size_hint(&self) -> (usize, Option<usize>) {
(self.0, self.1)
}
}
@@ -942,15 +970,14 @@ fn cloned<T, D>(self) -> Cloned<Self> where
/// Repeats an iterator endlessly
///
/// # Example
/// # Examples
///
/// ```rust
/// use std::iter::count;
///
/// let a = count(1i,1i).take(1);
/// let mut cy = a.cycle();
/// assert_eq!(cy.next(), Some(1));
/// assert_eq!(cy.next(), Some(1));
/// ```
/// let a = [1, 2];
/// let mut it = a.iter().cycle();
/// assert_eq!(it.next().unwrap(), &1);
/// assert_eq!(it.next().unwrap(), &2);
/// assert_eq!(it.next().unwrap(), &1);
/// ```
#[stable]
#[inline]
@@ -986,7 +1013,7 @@ pub trait DoubleEndedIterator: Iterator {
fn next_back(&mut self) -> Option<Self::Item>;
}
/// An object implementing random access indexing by `uint`
/// An object implementing random access indexing by `usize`
///
/// A `RandomAccessIterator` should be either infinite or a `DoubleEndedIterator`.
/// Calling `next()` or `next_back()` on a `RandomAccessIterator`
@@ -994,12 +1021,12 @@ pub trait DoubleEndedIterator: Iterator {
/// after `it.next()` is called.
#[unstable = "not widely used, may be better decomposed into Index and ExactSizeIterator"]
pub trait RandomAccessIterator: Iterator {
/// Return the number of indexable elements. At most `std::uint::MAX`
/// Return the number of indexable elements. At most `std::usize::MAX`
/// elements are indexable, even if the iterator represents a longer range.
fn indexable(&self) -> uint;
fn indexable(&self) -> usize;
/// Return an element at an index, or `None` if the index is out of bounds
fn idx(&mut self, index: uint) -> Option<Self::Item>;
fn idx(&mut self, index: usize) -> Option<Self::Item>;
}
/// An iterator that knows its exact length
@@ -1008,12 +1035,12 @@ pub trait RandomAccessIterator: Iterator {
/// it can support double-ended enumeration.
///
/// `Iterator::size_hint` *must* return the exact size of the iterator.
/// Note that the size must fit in `uint`.
/// Note that the size must fit in `usize`.
#[stable]
pub trait ExactSizeIterator: Iterator {
#[inline]
/// Return the exact length of the iterator.
fn len(&self) -> uint {
fn len(&self) -> usize {
let (lower, upper) = self.size_hint();
// Note: This assertion is overly defensive, but it checks the invariant
// guaranteed by the trait. If this trait were rust-internal,
@@ -1058,7 +1085,7 @@ impl<I> Iterator for Rev<I> where I: DoubleEndedIterator {
#[inline]
fn next(&mut self) -> Option<<I as Iterator>::Item> { self.iter.next_back() }
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
}
#[stable]
@@ -1070,9 +1097,9 @@ fn next_back(&mut self) -> Option<<I as Iterator>::Item> { self.iter.next() }
#[unstable = "trait is experimental"]
impl<I> RandomAccessIterator for Rev<I> where I: DoubleEndedIterator + RandomAccessIterator {
#[inline]
fn indexable(&self) -> uint { self.iter.indexable() }
fn indexable(&self) -> usize { self.iter.indexable() }
#[inline]
fn idx(&mut self, index: uint) -> Option<<I as Iterator>::Item> {
fn idx(&mut self, index: usize) -> Option<<I as Iterator>::Item> {
let amt = self.indexable();
self.iter.idx(amt - index - 1)
}
@@ -1092,7 +1119,7 @@ impl<'a, I> Iterator for ByRef<'a, I> where I: 'a + Iterator {
#[inline]
fn next(&mut self) -> Option<<I as Iterator>::Item> { self.iter.next() }
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
}
#[stable]
@@ -1106,9 +1133,9 @@ fn next_back(&mut self) -> Option<<I as Iterator>::Item> { self.iter.next_back()
pub trait AdditiveIterator<A> {
/// Iterates over the entire iterator, summing up all the elements
///
/// # Example
/// # Examples
///
/// ```rust
/// ```
/// use std::iter::AdditiveIterator;
///
/// let a = [1i, 2, 3, 4, 5];
@@ -1133,12 +1160,12 @@ fn sum(self) -> $A {
impl_additive! { i16, 0 }
impl_additive! { i32, 0 }
impl_additive! { i64, 0 }
impl_additive! { int, 0 }
impl_additive! { isize, 0 }
impl_additive! { u8, 0 }
impl_additive! { u16, 0 }
impl_additive! { u32, 0 }
impl_additive! { u64, 0 }
impl_additive! { uint, 0 }
impl_additive! { usize, 0 }
impl_additive! { f32, 0.0 }
impl_additive! { f64, 0.0 }
@@ -1147,12 +1174,12 @@ fn sum(self) -> $A {
pub trait MultiplicativeIterator<A> {
/// Iterates over the entire iterator, multiplying all the elements
///
/// # Example
/// # Examples
///
/// ```rust
/// ```
/// use std::iter::{count, MultiplicativeIterator};
///
/// fn factorial(n: uint) -> uint {
/// fn factorial(n: usize) -> usize {
/// count(1u, 1).take_while(|&i| i <= n).product()
/// }
/// assert!(factorial(0) == 1);
@@ -1177,12 +1204,12 @@ fn product(self) -> $A {
impl_multiplicative! { i16, 1 }
impl_multiplicative! { i32, 1 }
impl_multiplicative! { i64, 1 }
impl_multiplicative! { int, 1 }
impl_multiplicative! { isize, 1 }
impl_multiplicative! { u8, 1 }
impl_multiplicative! { u16, 1 }
impl_multiplicative! { u32, 1 }
impl_multiplicative! { u64, 1 }
impl_multiplicative! { uint, 1 }
impl_multiplicative! { usize, 1 }
impl_multiplicative! { f32, 1.0 }
impl_multiplicative! { f64, 1.0 }
@@ -1206,19 +1233,19 @@ impl<T: Clone> MinMaxResult<T> {
/// `Some(x,y)` is returned where `x <= y`. If `MinMaxResult` has variant `OneElement(x)`,
/// performing this operation will make one clone of `x`.
///
/// # Example
/// # Examples
///
/// ```rust
/// ```
/// use std::iter::MinMaxResult::{self, NoElements, OneElement, MinMax};
///
/// let r: MinMaxResult<int> = NoElements;
/// let r: MinMaxResult<isize> = NoElements;
/// assert_eq!(r.into_option(), None);
///
/// let r = OneElement(1i);
/// assert_eq!(r.into_option(), Some((1,1)));
/// let r = OneElement(1);
/// assert_eq!(r.into_option(), Some((1, 1)));
///
/// let r = MinMax(1i,2i);
/// assert_eq!(r.into_option(), Some((1,2)));
/// let r = MinMax(1, 2);
/// assert_eq!(r.into_option(), Some((1, 2)));
/// ```
#[unstable = "type is unstable"]
pub fn into_option(self) -> Option<(T,T)> {
@@ -1250,7 +1277,7 @@ fn next(&mut self) -> Option<T> {
self.it.next().cloned()
}
fn size_hint(&self) -> (uint, Option<uint>) {
fn size_hint(&self) -> (usize, Option<usize>) {
self.it.size_hint()
}
}
@@ -1295,12 +1322,12 @@ fn next(&mut self) -> Option<<I as Iterator>::Item> {
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
fn size_hint(&self) -> (usize, Option<usize>) {
// the cycle iterator is either empty or infinite
match self.orig.size_hint() {
sz @ (0, Some(0)) => sz,
(0, _) => (0, None),
_ => (uint::MAX, None)
_ => (usize::MAX, None)
}
}
}
@@ -1310,16 +1337,16 @@ impl<I> RandomAccessIterator for Cycle<I> where
I: Clone + RandomAccessIterator,
{
#[inline]
fn indexable(&self) -> uint {
fn indexable(&self) -> usize {
if self.orig.indexable() > 0 {
uint::MAX
usize::MAX
} else {
0
}
}
#[inline]
fn idx(&mut self, index: uint) -> Option<<I as Iterator>::Item> {
fn idx(&mut self, index: usize) -> Option<<I as Iterator>::Item> {
let liter = self.iter.indexable();
let lorig = self.orig.indexable();
if lorig == 0 {
@@ -1361,7 +1388,7 @@ fn next(&mut self) -> Option<T> {
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
fn size_hint(&self) -> (usize, Option<usize>) {
let (a_lower, a_upper) = self.a.size_hint();
let (b_lower, b_upper) = self.b.size_hint();
@@ -1396,13 +1423,13 @@ impl<T, A, B> RandomAccessIterator for Chain<A, B> where
B: RandomAccessIterator<Item=T>,
{
#[inline]
fn indexable(&self) -> uint {
fn indexable(&self) -> usize {
let (a, b) = (self.a.indexable(), self.b.indexable());
a.saturating_add(b)
}
#[inline]
fn idx(&mut self, index: uint) -> Option<T> {
fn idx(&mut self, index: usize) -> Option<T> {
let len = self.a.indexable();
if index < len {
self.a.idx(index)
@@ -1440,7 +1467,7 @@ fn next(&mut self) -> Option<(T, U)> {
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
fn size_hint(&self) -> (usize, Option<usize>) {
let (a_lower, a_upper) = self.a.size_hint();
let (b_lower, b_upper) = self.b.size_hint();
@@ -1488,12 +1515,12 @@ impl<T, U, A, B> RandomAccessIterator for Zip<A, B> where
B: RandomAccessIterator<Item=U>,
{
#[inline]
fn indexable(&self) -> uint {
fn indexable(&self) -> usize {
cmp::min(self.a.indexable(), self.b.indexable())
}
#[inline]
fn idx(&mut self, index: uint) -> Option<(T, U)> {
fn idx(&mut self, index: usize) -> Option<(T, U)> {
match self.a.idx(index) {
None => None,
Some(x) => match self.b.idx(index) {
@@ -1547,7 +1574,7 @@ fn next(&mut self) -> Option<B> {
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
@@ -1570,12 +1597,12 @@ impl<A, B, I, F> RandomAccessIterator for Map<A, B, I, F> where
F: FnMut(A) -> B,
{
#[inline]
fn indexable(&self) -> uint {
fn indexable(&self) -> usize {
self.iter.indexable()
}
#[inline]
fn idx(&mut self, index: uint) -> Option<B> {
fn idx(&mut self, index: usize) -> Option<B> {
let elt = self.iter.idx(index);
self.do_map(elt)
}
@@ -1620,7 +1647,7 @@ fn next(&mut self) -> Option<A> {
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
fn size_hint(&self) -> (usize, Option<usize>) {
let (_, upper) = self.iter.size_hint();
(0, upper) // can't know a lower bound, due to the predicate
}
@@ -1683,7 +1710,7 @@ fn next(&mut self) -> Option<B> {
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
fn size_hint(&self) -> (usize, Option<usize>) {
let (_, upper) = self.iter.size_hint();
(0, upper) // can't know a lower bound, due to the predicate
}
@@ -1712,15 +1739,15 @@ fn next_back(&mut self) -> Option<B> {
#[stable]
pub struct Enumerate<I> {
iter: I,
count: uint
count: usize
}
#[stable]
impl<I> Iterator for Enumerate<I> where I: Iterator {
type Item = (uint, <I as Iterator>::Item);
type Item = (usize, <I as Iterator>::Item);
#[inline]
fn next(&mut self) -> Option<(uint, <I as Iterator>::Item)> {
fn next(&mut self) -> Option<(usize, <I as Iterator>::Item)> {
match self.iter.next() {
Some(a) => {
let ret = Some((self.count, a));
@@ -1732,7 +1759,7 @@ fn next(&mut self) -> Option<(uint, <I as Iterator>::Item)> {
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
@@ -1742,7 +1769,7 @@ impl<I> DoubleEndedIterator for Enumerate<I> where
I: ExactSizeIterator + DoubleEndedIterator
{
#[inline]
fn next_back(&mut self) -> Option<(uint, <I as Iterator>::Item)> {
fn next_back(&mut self) -> Option<(usize, <I as Iterator>::Item)> {
match self.iter.next_back() {
Some(a) => {
let len = self.iter.len();
@@ -1756,12 +1783,12 @@ fn next_back(&mut self) -> Option<(uint, <I as Iterator>::Item)> {
#[unstable = "trait is experimental"]
impl<I> RandomAccessIterator for Enumerate<I> where I: RandomAccessIterator {
#[inline]
fn indexable(&self) -> uint {
fn indexable(&self) -> usize {
self.iter.indexable()
}
#[inline]
fn idx(&mut self, index: uint) -> Option<(uint, <I as Iterator>::Item)> {
fn idx(&mut self, index: usize) -> Option<(usize, <I as Iterator>::Item)> {
match self.iter.idx(index) {
Some(a) => Some((self.count + index, a)),
_ => None,
@@ -1789,7 +1816,7 @@ fn next(&mut self) -> Option<T> {
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
fn size_hint(&self) -> (usize, Option<usize>) {
let (lo, hi) = self.iter.size_hint();
if self.peeked.is_some() {
let lo = lo.saturating_add(1);
@@ -1866,7 +1893,7 @@ fn next(&mut self) -> Option<A> {
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
fn size_hint(&self) -> (usize, Option<usize>) {
let (_, upper) = self.iter.size_hint();
(0, upper) // can't know a lower bound, due to the predicate
}
@@ -1920,7 +1947,7 @@ fn next(&mut self) -> Option<A> {
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
fn size_hint(&self) -> (usize, Option<usize>) {
let (_, upper) = self.iter.size_hint();
(0, upper) // can't know a lower bound, due to the predicate
}
@@ -1932,7 +1959,7 @@ fn size_hint(&self) -> (uint, Option<uint>) {
#[stable]
pub struct Skip<I> {
iter: I,
n: uint
n: usize
}
#[stable]
@@ -1965,7 +1992,7 @@ fn next(&mut self) -> Option<<I as Iterator>::Item> {
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
fn size_hint(&self) -> (usize, Option<usize>) {
let (lower, upper) = self.iter.size_hint();
let lower = lower.saturating_sub(self.n);
@@ -1982,12 +2009,12 @@ fn size_hint(&self) -> (uint, Option<uint>) {
#[unstable = "trait is experimental"]
impl<I> RandomAccessIterator for Skip<I> where I: RandomAccessIterator{
#[inline]
fn indexable(&self) -> uint {
fn indexable(&self) -> usize {
self.iter.indexable().saturating_sub(self.n)
}
#[inline]
fn idx(&mut self, index: uint) -> Option<<I as Iterator>::Item> {
fn idx(&mut self, index: usize) -> Option<<I as Iterator>::Item> {
if index >= self.indexable() {
None
} else {
@@ -2002,7 +2029,7 @@ fn idx(&mut self, index: uint) -> Option<<I as Iterator>::Item> {
#[stable]
pub struct Take<I> {
iter: I,
n: uint
n: usize
}
#[stable]
@@ -2020,7 +2047,7 @@ fn next(&mut self) -> Option<<I as Iterator>::Item> {
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
fn size_hint(&self) -> (usize, Option<usize>) {
let (lower, upper) = self.iter.size_hint();
let lower = cmp::min(lower, self.n);
@@ -2037,12 +2064,12 @@ fn size_hint(&self) -> (uint, Option<uint>) {
#[unstable = "trait is experimental"]
impl<I> RandomAccessIterator for Take<I> where I: RandomAccessIterator{
#[inline]
fn indexable(&self) -> uint {
fn indexable(&self) -> usize {
cmp::min(self.iter.indexable(), self.n)
}
#[inline]
fn idx(&mut self, index: uint) -> Option<<I as Iterator>::Item> {
fn idx(&mut self, index: usize) -> Option<<I as Iterator>::Item> {
if index >= self.n {
None
} else {
@@ -2092,7 +2119,7 @@ fn next(&mut self) -> Option<B> {
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
fn size_hint(&self) -> (usize, Option<usize>) {
let (_, upper) = self.iter.size_hint();
(0, upper) // can't know a lower bound, due to the scan function
}
@@ -2155,7 +2182,7 @@ fn next(&mut self) -> Option<B> {
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
fn size_hint(&self) -> (usize, Option<usize>) {
let (flo, fhi) = self.frontiter.as_ref().map_or((0, Some(0)), |it| it.size_hint());
let (blo, bhi) = self.backiter.as_ref().map_or((0, Some(0)), |it| it.size_hint());
let lo = flo.saturating_add(blo);
@@ -2219,7 +2246,7 @@ fn next(&mut self) -> Option<<I as Iterator>::Item> {
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
fn size_hint(&self) -> (usize, Option<usize>) {
if self.done {
(0, Some(0))
} else {
@@ -2250,12 +2277,12 @@ fn next_back(&mut self) -> Option<<I as Iterator>::Item> {
#[unstable = "trait is experimental"]
impl<I> RandomAccessIterator for Fuse<I> where I: RandomAccessIterator {
#[inline]
fn indexable(&self) -> uint {
fn indexable(&self) -> usize {
self.iter.indexable()
}
#[inline]
fn idx(&mut self, index: uint) -> Option<<I as Iterator>::Item> {
fn idx(&mut self, index: usize) -> Option<<I as Iterator>::Item> {
self.iter.idx(index)
}
}
@@ -2316,7 +2343,7 @@ fn next(&mut self) -> Option<A> {
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
@@ -2339,12 +2366,12 @@ impl<A, I, F> RandomAccessIterator for Inspect<A, I, F> where
F: FnMut(&A),
{
#[inline]
fn indexable(&self) -> uint {
fn indexable(&self) -> usize {
self.iter.indexable()
}
#[inline]
fn idx(&mut self, index: uint) -> Option<A> {
fn idx(&mut self, index: usize) -> Option<A> {
let element = self.iter.idx(index);
self.do_inspect(element)
}
@@ -2356,7 +2383,7 @@ fn idx(&mut self, index: uint) -> Option<A> {
///
/// An iterator that yields sequential Fibonacci numbers, and stops on overflow.
///
/// ```rust
/// ```
/// use std::iter::Unfold;
/// use std::num::Int; // For `.checked_add()`
///
@@ -2426,7 +2453,7 @@ fn next(&mut self) -> Option<A> {
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
fn size_hint(&self) -> (usize, Option<usize>) {
// no possible known bounds at this point
(0, None)
}
@@ -2462,8 +2489,8 @@ fn next(&mut self) -> Option<A> {
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
(uint::MAX, None) // Too bad we can't specify an infinite lower bound
fn size_hint(&self) -> (usize, Option<usize>) {
(usize::MAX, None) // Too bad we can't specify an infinite lower bound
}
}
@@ -2479,9 +2506,9 @@ pub struct Range<A> {
/// Returns an iterator over the given range [start, stop) (that is, starting
/// at start (inclusive), and ending at stop (exclusive)).
///
/// # Example
/// # Examples
///
/// ```rust
/// ```
/// let array = [0, 1, 2, 3, 4];
///
/// for i in range(0, 5u) {
@@ -2516,9 +2543,9 @@ fn next(&mut self) -> Option<A> {
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
fn size_hint(&self) -> (usize, Option<usize>) {
// This first checks if the elements are representable as i64. If they aren't, try u64 (to
// handle cases like range(huge, huger)). We don't use uint/int because the difference of
// handle cases like range(huge, huger)). We don't use usize/isize because the difference of
// the i64/u64 might lie within their range.
let bound = match self.state.to_i64() {
Some(a) => {
@@ -2601,7 +2628,7 @@ fn next(&mut self) -> Option<A> {
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
fn size_hint(&self) -> (usize, Option<usize>) {
let (lo, hi) = self.range.size_hint();
if self.done {
(lo, hi)
@@ -2733,9 +2760,9 @@ fn next(&mut self) -> Option<$t> {
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
fn size_hint(&self) -> (usize, Option<usize>) {
debug_assert!(self.end >= self.start);
let hint = (self.end - self.start) as uint;
let hint = (self.end - self.start) as usize;
(hint, Some(hint))
}
}
@@ -2795,13 +2822,13 @@ fn next(&mut self) -> Option<$t> {
)*)
}
range_impl!(uint u8 u16 u32 int i8 i16 i32);
range_impl!(usize u8 u16 u32 isize i8 i16 i32);
#[cfg(target_pointer_width = "64")]
range_impl!(u64 i64);
#[cfg(target_pointer_width = "32")]
range_impl_no_hint!(u64 i64);
range_other_impls!(uint u8 u16 u32 u64 int i8 i16 i32 i64);
range_other_impls!(usize u8 u16 u32 u64 isize i8 i16 i32 i64);
/// An iterator that repeats an element endlessly
#[derive(Clone)]
@@ -2817,7 +2844,7 @@ impl<A: Clone> Iterator for Repeat<A> {
#[inline]
fn next(&mut self) -> Option<A> { self.idx(0) }
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) { (uint::MAX, None) }
fn size_hint(&self) -> (usize, Option<usize>) { (usize::MAX, None) }
}
#[stable]
@@ -2829,9 +2856,9 @@ fn next_back(&mut self) -> Option<A> { self.idx(0) }
#[unstable = "trait is experimental"]
impl<A: Clone> RandomAccessIterator for Repeat<A> {
#[inline]
fn indexable(&self) -> uint { uint::MAX }
fn indexable(&self) -> usize { usize::MAX }
#[inline]
fn idx(&mut self, _: uint) -> Option<A> { Some(self.element.clone()) }
fn idx(&mut self, _: usize) -> Option<A> { Some(self.element.clone()) }
}
type IterateState<T, F> = (F, Option<T>, bool);