From 9091e047e3b3e56fee78d1ae2ba7284ee894fd37 Mon Sep 17 00:00:00 2001 From: Tshepang Lekhonkhobe Date: Mon, 19 Jan 2015 02:37:21 +0200 Subject: [PATCH] iter: miscellaneous improvements There are doc improvements as well as these conversions: - int/uint -> isize/usize - u/i -> us/is --- src/libcore/iter.rs | 499 +++++++++++++++++++++++--------------------- 1 file changed, 263 insertions(+), 236 deletions(-) diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index 773ac99b0dee2..fc3a8ae6590de 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -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 ops::{Add, Deref, FnMut}; 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) { (0, None) } + fn size_hint(&self) -> (usize, Option) { (0, None) } } // FIXME(#21363) remove `old_impl_check` when bug is fixed @@ -135,26 +135,25 @@ pub trait Extend { 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 @@ pub trait IteratorExt: Iterator + Sized { /// 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 { + fn nth(&mut self, mut n: usize) -> Option { for x in *self { if n == 0 { return Some(x) } n -= 1; @@ -190,11 +189,11 @@ pub trait IteratorExt: Iterator + Sized { /// 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 @@ pub trait IteratorExt: Iterator + Sized { /// 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 @@ pub trait IteratorExt: Iterator + Sized { /// 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 @@ pub trait IteratorExt: Iterator + Sized { } /// 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 @@ pub trait IteratorExt: Iterator + Sized { /// 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 @@ pub trait IteratorExt: Iterator + Sized { /// 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 @@ pub trait IteratorExt: Iterator + Sized { /// 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 @@ pub trait IteratorExt: Iterator + Sized { /// 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 @@ pub trait IteratorExt: Iterator + Sized { /// 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 @@ pub trait IteratorExt: Iterator + Sized { /// 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 @@ pub trait IteratorExt: Iterator + Sized { /// ``` #[inline] #[stable] - fn skip(self, n: uint) -> Skip { + fn skip(self, n: usize) -> Skip { 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 @@ pub trait IteratorExt: Iterator + Sized { /// ``` #[inline] #[stable] - fn take(self, n: uint) -> Take { + fn take(self, n: usize) -> Take { Take{iter: self, n: n} } @@ -418,10 +415,10 @@ pub trait IteratorExt: Iterator + Sized { /// 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 @@ pub trait IteratorExt: Iterator + Sized { } /// 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 @@ pub trait IteratorExt: Iterator + Sized { /// iterator yields `None`. Random-access iterator behavior is not /// affected, only single and double-ended iterator behavior. /// - /// # Example + /// # Examples /// - /// ```rust - /// fn process>(it: U) -> int { + /// ``` + /// fn process>(it: U) -> isize { /// let mut it = it.fuse(); /// let mut sum = 0; /// for x in it { @@ -494,9 +487,9 @@ pub trait IteratorExt: Iterator + Sized { /// } /// 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 @@ pub trait IteratorExt: Iterator + Sized { /// 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 @@ pub trait IteratorExt: Iterator + Sized { /// 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 @@ pub trait IteratorExt: Iterator + Sized { /// 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 = 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 @@ pub trait IteratorExt: Iterator + Sized { /// do not. /// /// ``` - /// let vec = vec![1i, 2i, 3i, 4i]; - /// let (even, odd): (Vec, Vec) = 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 @@ pub trait IteratorExt: Iterator + Sized { /// 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 @@ pub trait IteratorExt: Iterator + Sized { /// 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 @@ pub trait IteratorExt: Iterator + Sized { 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 @@ pub trait IteratorExt: Iterator + Sized { /// 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

(&mut self, mut predicate: P) -> Option where @@ -667,9 +669,19 @@ pub trait IteratorExt: Iterator + Sized { } /// 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

(&mut self, mut predicate: P) -> Option where + fn position

(&mut self, mut predicate: P) -> Option where P: FnMut(Self::Item) -> bool, { let mut i = 0; @@ -685,9 +697,19 @@ pub trait IteratorExt: Iterator + Sized { /// 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

(&mut self, mut predicate: P) -> Option where + fn rposition

(&mut self, mut predicate: P) -> Option where P: FnMut(Self::Item) -> bool, Self: ExactSizeIterator + DoubleEndedIterator { @@ -702,10 +724,10 @@ pub trait IteratorExt: Iterator + Sized { /// 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 @@ pub trait IteratorExt: Iterator + Sized { /// 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 @@ pub trait IteratorExt: Iterator + Sized { /// 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 v = [1i]; - /// assert!(v.iter().min_max() == OneElement(&1)); + /// let a: [isize; 0] = []; + /// assert_eq!(a.iter().min_max(), NoElements); /// - /// let v = [1i, 2, 3, 4, 5]; - /// assert!(v.iter().min_max() == MinMax(&1, &5)); + /// let a = [1]; + /// assert!(a.iter().min_max() == OneElement(&1)); /// - /// let v = [1i, 2, 3, 4, 5, 6]; - /// assert!(v.iter().min_max() == MinMax(&1, &6)); + /// let a = [1, 2, 3, 4, 5]; + /// assert!(a.iter().min_max() == MinMax(&1, &5)); /// - /// 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 where Self::Item: Ord @@ -821,13 +840,13 @@ pub trait IteratorExt: Iterator + Sized { /// 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 @@ pub trait IteratorExt: Iterator + Sized { /// 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 @@ pub trait IteratorExt: Iterator + Sized { /// 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 { @@ -898,18 +917,27 @@ pub trait IteratorExt: Iterator + Sized { /// /// 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(mut self) -> (FromA, FromB) where FromA: Default + Extend, FromB: Default + Extend, Self: Iterator, { - struct SizeHint(uint, Option); + struct SizeHint(usize, Option); impl Iterator for SizeHint { type Item = A; fn next(&mut self) -> Option { None } - fn size_hint(&self) -> (uint, Option) { + fn size_hint(&self) -> (usize, Option) { (self.0, self.1) } } @@ -942,15 +970,14 @@ pub trait IteratorExt: Iterator + Sized { /// 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; } -/// 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; + fn idx(&mut self, index: usize) -> Option; } /// 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 Iterator for Rev where I: DoubleEndedIterator { #[inline] fn next(&mut self) -> Option<::Item> { self.iter.next_back() } #[inline] - fn size_hint(&self) -> (uint, Option) { self.iter.size_hint() } + fn size_hint(&self) -> (usize, Option) { self.iter.size_hint() } } #[stable] @@ -1070,9 +1097,9 @@ impl DoubleEndedIterator for Rev where I: DoubleEndedIterator { #[unstable = "trait is experimental"] impl RandomAccessIterator for Rev 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<::Item> { + fn idx(&mut self, index: usize) -> Option<::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<::Item> { self.iter.next() } #[inline] - fn size_hint(&self) -> (uint, Option) { self.iter.size_hint() } + fn size_hint(&self) -> (usize, Option) { self.iter.size_hint() } } #[stable] @@ -1106,9 +1133,9 @@ impl<'a, I> DoubleEndedIterator for ByRef<'a, I> where I: 'a + DoubleEndedIterat pub trait AdditiveIterator { /// 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 @@ impl_additive! { i8, 0 } 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 @@ impl_additive! { f64, 0.0 } pub trait MultiplicativeIterator { /// 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 @@ impl_multiplicative! { i8, 1 } 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 MinMaxResult { /// `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 = NoElements; + /// let r: MinMaxResult = 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 @@ impl Iterator for Cloned where self.it.next().cloned() } - fn size_hint(&self) -> (uint, Option) { + fn size_hint(&self) -> (usize, Option) { self.it.size_hint() } } @@ -1295,12 +1322,12 @@ impl Iterator for Cycle where I: Clone + Iterator { } #[inline] - fn size_hint(&self) -> (uint, Option) { + fn size_hint(&self) -> (usize, Option) { // 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 RandomAccessIterator for Cycle 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<::Item> { + fn idx(&mut self, index: usize) -> Option<::Item> { let liter = self.iter.indexable(); let lorig = self.orig.indexable(); if lorig == 0 { @@ -1361,7 +1388,7 @@ impl Iterator for Chain where A: Iterator, B: Iterator (uint, Option) { + fn size_hint(&self) -> (usize, Option) { let (a_lower, a_upper) = self.a.size_hint(); let (b_lower, b_upper) = self.b.size_hint(); @@ -1396,13 +1423,13 @@ impl RandomAccessIterator for Chain where B: RandomAccessIterator, { #[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 { + fn idx(&mut self, index: usize) -> Option { let len = self.a.indexable(); if index < len { self.a.idx(index) @@ -1440,7 +1467,7 @@ impl Iterator for Zip where } #[inline] - fn size_hint(&self) -> (uint, Option) { + fn size_hint(&self) -> (usize, Option) { let (a_lower, a_upper) = self.a.size_hint(); let (b_lower, b_upper) = self.b.size_hint(); @@ -1488,12 +1515,12 @@ impl RandomAccessIterator for Zip where B: RandomAccessIterator, { #[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 @@ impl Iterator for Map where I: Iterator, F: FnMu } #[inline] - fn size_hint(&self) -> (uint, Option) { + fn size_hint(&self) -> (usize, Option) { self.iter.size_hint() } } @@ -1570,12 +1597,12 @@ impl RandomAccessIterator for Map 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 { + fn idx(&mut self, index: usize) -> Option { let elt = self.iter.idx(index); self.do_map(elt) } @@ -1620,7 +1647,7 @@ impl Iterator for Filter where I: Iterator, P: FnMut(& } #[inline] - fn size_hint(&self) -> (uint, Option) { + fn size_hint(&self) -> (usize, Option) { let (_, upper) = self.iter.size_hint(); (0, upper) // can't know a lower bound, due to the predicate } @@ -1683,7 +1710,7 @@ impl Iterator for FilterMap where } #[inline] - fn size_hint(&self) -> (uint, Option) { + fn size_hint(&self) -> (usize, Option) { let (_, upper) = self.iter.size_hint(); (0, upper) // can't know a lower bound, due to the predicate } @@ -1712,15 +1739,15 @@ impl DoubleEndedIterator for FilterMap where #[stable] pub struct Enumerate { iter: I, - count: uint + count: usize } #[stable] impl Iterator for Enumerate where I: Iterator { - type Item = (uint, ::Item); + type Item = (usize, ::Item); #[inline] - fn next(&mut self) -> Option<(uint, ::Item)> { + fn next(&mut self) -> Option<(usize, ::Item)> { match self.iter.next() { Some(a) => { let ret = Some((self.count, a)); @@ -1732,7 +1759,7 @@ impl Iterator for Enumerate where I: Iterator { } #[inline] - fn size_hint(&self) -> (uint, Option) { + fn size_hint(&self) -> (usize, Option) { self.iter.size_hint() } } @@ -1742,7 +1769,7 @@ impl DoubleEndedIterator for Enumerate where I: ExactSizeIterator + DoubleEndedIterator { #[inline] - fn next_back(&mut self) -> Option<(uint, ::Item)> { + fn next_back(&mut self) -> Option<(usize, ::Item)> { match self.iter.next_back() { Some(a) => { let len = self.iter.len(); @@ -1756,12 +1783,12 @@ impl DoubleEndedIterator for Enumerate where #[unstable = "trait is experimental"] impl RandomAccessIterator for Enumerate where I: RandomAccessIterator { #[inline] - fn indexable(&self) -> uint { + fn indexable(&self) -> usize { self.iter.indexable() } #[inline] - fn idx(&mut self, index: uint) -> Option<(uint, ::Item)> { + fn idx(&mut self, index: usize) -> Option<(usize, ::Item)> { match self.iter.idx(index) { Some(a) => Some((self.count + index, a)), _ => None, @@ -1789,7 +1816,7 @@ impl Iterator for Peekable where I: Iterator { } #[inline] - fn size_hint(&self) -> (uint, Option) { + fn size_hint(&self) -> (usize, Option) { let (lo, hi) = self.iter.size_hint(); if self.peeked.is_some() { let lo = lo.saturating_add(1); @@ -1866,7 +1893,7 @@ impl Iterator for SkipWhile where I: Iterator, P: FnMu } #[inline] - fn size_hint(&self) -> (uint, Option) { + fn size_hint(&self) -> (usize, Option) { let (_, upper) = self.iter.size_hint(); (0, upper) // can't know a lower bound, due to the predicate } @@ -1920,7 +1947,7 @@ impl Iterator for TakeWhile where I: Iterator, P: FnMu } #[inline] - fn size_hint(&self) -> (uint, Option) { + fn size_hint(&self) -> (usize, Option) { let (_, upper) = self.iter.size_hint(); (0, upper) // can't know a lower bound, due to the predicate } @@ -1932,7 +1959,7 @@ impl Iterator for TakeWhile where I: Iterator, P: FnMu #[stable] pub struct Skip { iter: I, - n: uint + n: usize } #[stable] @@ -1965,7 +1992,7 @@ impl Iterator for Skip where I: Iterator { } #[inline] - fn size_hint(&self) -> (uint, Option) { + fn size_hint(&self) -> (usize, Option) { let (lower, upper) = self.iter.size_hint(); let lower = lower.saturating_sub(self.n); @@ -1982,12 +2009,12 @@ impl Iterator for Skip where I: Iterator { #[unstable = "trait is experimental"] impl RandomAccessIterator for Skip 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<::Item> { + fn idx(&mut self, index: usize) -> Option<::Item> { if index >= self.indexable() { None } else { @@ -2002,7 +2029,7 @@ impl RandomAccessIterator for Skip where I: RandomAccessIterator{ #[stable] pub struct Take { iter: I, - n: uint + n: usize } #[stable] @@ -2020,7 +2047,7 @@ impl Iterator for Take where I: Iterator{ } #[inline] - fn size_hint(&self) -> (uint, Option) { + fn size_hint(&self) -> (usize, Option) { let (lower, upper) = self.iter.size_hint(); let lower = cmp::min(lower, self.n); @@ -2037,12 +2064,12 @@ impl Iterator for Take where I: Iterator{ #[unstable = "trait is experimental"] impl RandomAccessIterator for Take 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<::Item> { + fn idx(&mut self, index: usize) -> Option<::Item> { if index >= self.n { None } else { @@ -2092,7 +2119,7 @@ impl Iterator for Scan where } #[inline] - fn size_hint(&self) -> (uint, Option) { + fn size_hint(&self) -> (usize, Option) { let (_, upper) = self.iter.size_hint(); (0, upper) // can't know a lower bound, due to the scan function } @@ -2155,7 +2182,7 @@ impl Iterator for FlatMap where } #[inline] - fn size_hint(&self) -> (uint, Option) { + fn size_hint(&self) -> (usize, Option) { 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 @@ impl Iterator for Fuse where I: Iterator { } #[inline] - fn size_hint(&self) -> (uint, Option) { + fn size_hint(&self) -> (usize, Option) { if self.done { (0, Some(0)) } else { @@ -2250,12 +2277,12 @@ impl DoubleEndedIterator for Fuse where I: DoubleEndedIterator { #[unstable = "trait is experimental"] impl RandomAccessIterator for Fuse where I: RandomAccessIterator { #[inline] - fn indexable(&self) -> uint { + fn indexable(&self) -> usize { self.iter.indexable() } #[inline] - fn idx(&mut self, index: uint) -> Option<::Item> { + fn idx(&mut self, index: usize) -> Option<::Item> { self.iter.idx(index) } } @@ -2316,7 +2343,7 @@ impl Iterator for Inspect where I: Iterator, F: FnMut( } #[inline] - fn size_hint(&self) -> (uint, Option) { + fn size_hint(&self) -> (usize, Option) { self.iter.size_hint() } } @@ -2339,12 +2366,12 @@ impl RandomAccessIterator for Inspect where F: FnMut(&A), { #[inline] - fn indexable(&self) -> uint { + fn indexable(&self) -> usize { self.iter.indexable() } #[inline] - fn idx(&mut self, index: uint) -> Option { + fn idx(&mut self, index: usize) -> Option { let element = self.iter.idx(index); self.do_inspect(element) } @@ -2356,7 +2383,7 @@ impl RandomAccessIterator for Inspect where /// /// 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 @@ impl Iterator for Unfold where F: FnMut(&mut St) -> Option (uint, Option) { + fn size_hint(&self) -> (usize, Option) { // no possible known bounds at this point (0, None) } @@ -2462,8 +2489,8 @@ impl + Clone> Iterator for Counter { } #[inline] - fn size_hint(&self) -> (uint, Option) { - (uint::MAX, None) // Too bad we can't specify an infinite lower bound + fn size_hint(&self) -> (usize, Option) { + (usize::MAX, None) // Too bad we can't specify an infinite lower bound } } @@ -2479,9 +2506,9 @@ pub struct Range { /// 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 @@ impl Iterator for Range { } #[inline] - fn size_hint(&self) -> (uint, Option) { + fn size_hint(&self) -> (usize, Option) { // 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 @@ impl Iterator for RangeInclusive { } #[inline] - fn size_hint(&self) -> (uint, Option) { + fn size_hint(&self) -> (usize, Option) { let (lo, hi) = self.range.size_hint(); if self.done { (lo, hi) @@ -2733,9 +2760,9 @@ macro_rules! range_impl { } #[inline] - fn size_hint(&self) -> (uint, Option) { + fn size_hint(&self) -> (usize, Option) { 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 @@ macro_rules! range_other_impls { )*) } -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 Iterator for Repeat { #[inline] fn next(&mut self) -> Option { self.idx(0) } #[inline] - fn size_hint(&self) -> (uint, Option) { (uint::MAX, None) } + fn size_hint(&self) -> (usize, Option) { (usize::MAX, None) } } #[stable] @@ -2829,9 +2856,9 @@ impl DoubleEndedIterator for Repeat { #[unstable = "trait is experimental"] impl RandomAccessIterator for Repeat { #[inline] - fn indexable(&self) -> uint { uint::MAX } + fn indexable(&self) -> usize { usize::MAX } #[inline] - fn idx(&mut self, _: uint) -> Option { Some(self.element.clone()) } + fn idx(&mut self, _: usize) -> Option { Some(self.element.clone()) } } type IterateState = (F, Option, bool);