|
11 | 11 | use clone::Clone;
|
12 | 12 | use cmp::{Ord, PartialOrd, PartialEq, Ordering};
|
13 | 13 | use default::Default;
|
14 |
| -use num::{Zero, One}; |
15 |
| -use ops::{Add, FnMut, Mul}; |
| 14 | +use ops::FnMut; |
16 | 15 | use option::Option::{self, Some, None};
|
17 | 16 | use marker::Sized;
|
18 | 17 |
|
19 |
| -use super::{Chain, Cycle, Cloned, Enumerate, Filter, FilterMap, FlatMap, Fuse, |
20 |
| - Inspect, Map, Peekable, Scan, Skip, SkipWhile, Take, TakeWhile, Rev, |
21 |
| - Zip}; |
| 18 | +use super::{Chain, Cycle, Cloned, Enumerate, Filter, FilterMap, FlatMap, Fuse}; |
| 19 | +use super::{Inspect, Map, Peekable, Scan, Skip, SkipWhile, Take, TakeWhile, Rev}; |
| 20 | +use super::{Zip, Sum, Product}; |
22 | 21 | use super::ChainState;
|
23 |
| -use super::{DoubleEndedIterator, ExactSizeIterator, Extend, FromIterator, |
24 |
| - IntoIterator}; |
25 |
| -use super::ZipImpl; |
| 22 | +use super::{DoubleEndedIterator, ExactSizeIterator, Extend, FromIterator}; |
| 23 | +use super::{IntoIterator, ZipImpl}; |
26 | 24 |
|
27 | 25 | fn _assert_is_object_safe(_: &Iterator<Item=()>) {}
|
28 | 26 |
|
@@ -1820,50 +1818,54 @@ pub trait Iterator {
|
1820 | 1818 | ///
|
1821 | 1819 | /// An empty iterator returns the zero value of the type.
|
1822 | 1820 | ///
|
| 1821 | + /// # Panics |
| 1822 | + /// |
| 1823 | + /// When calling `sum` and a primitive integer type is being returned, this |
| 1824 | + /// method will panic if the computation overflows. |
| 1825 | + /// |
1823 | 1826 | /// # Examples
|
1824 | 1827 | ///
|
1825 | 1828 | /// Basic usage:
|
1826 | 1829 | ///
|
1827 | 1830 | /// ```
|
1828 |
| - /// #![feature(iter_arith)] |
1829 |
| - /// |
1830 | 1831 | /// let a = [1, 2, 3];
|
1831 | 1832 | /// let sum: i32 = a.iter().sum();
|
1832 | 1833 | ///
|
1833 | 1834 | /// assert_eq!(sum, 6);
|
1834 | 1835 | /// ```
|
1835 |
| - #[unstable(feature = "iter_arith", reason = "bounds recently changed", |
1836 |
| - issue = "27739")] |
1837 |
| - fn sum<S>(self) -> S where |
1838 |
| - S: Add<Self::Item, Output=S> + Zero, |
1839 |
| - Self: Sized, |
| 1836 | + #[stable(feature = "iter_arith", since = "1.11.0")] |
| 1837 | + fn sum<S>(self) -> S |
| 1838 | + where Self: Sized, |
| 1839 | + S: Sum<Self::Item>, |
1840 | 1840 | {
|
1841 |
| - self.fold(Zero::zero(), |s, e| s + e) |
| 1841 | + Sum::sum(self) |
1842 | 1842 | }
|
1843 | 1843 |
|
1844 | 1844 | /// Iterates over the entire iterator, multiplying all the elements
|
1845 | 1845 | ///
|
1846 | 1846 | /// An empty iterator returns the one value of the type.
|
1847 | 1847 | ///
|
| 1848 | + /// # Panics |
| 1849 | + /// |
| 1850 | + /// When calling `product` and a primitive integer type is being returned, |
| 1851 | + /// this method will panic if the computation overflows. |
| 1852 | + /// |
1848 | 1853 | /// # Examples
|
1849 | 1854 | ///
|
1850 | 1855 | /// ```
|
1851 |
| - /// #![feature(iter_arith)] |
1852 |
| - /// |
1853 | 1856 | /// fn factorial(n: u32) -> u32 {
|
1854 | 1857 | /// (1..).take_while(|&i| i <= n).product()
|
1855 | 1858 | /// }
|
1856 | 1859 | /// assert_eq!(factorial(0), 1);
|
1857 | 1860 | /// assert_eq!(factorial(1), 1);
|
1858 | 1861 | /// assert_eq!(factorial(5), 120);
|
1859 | 1862 | /// ```
|
1860 |
| - #[unstable(feature="iter_arith", reason = "bounds recently changed", |
1861 |
| - issue = "27739")] |
1862 |
| - fn product<P>(self) -> P where |
1863 |
| - P: Mul<Self::Item, Output=P> + One, |
1864 |
| - Self: Sized, |
| 1863 | + #[stable(feature = "iter_arith", since = "1.11.0")] |
| 1864 | + fn product<P>(self) -> P |
| 1865 | + where Self: Sized, |
| 1866 | + P: Product<Self::Item>, |
1865 | 1867 | {
|
1866 |
| - self.fold(One::one(), |p, e| p * e) |
| 1868 | + Product::product(self) |
1867 | 1869 | }
|
1868 | 1870 |
|
1869 | 1871 | /// Lexicographically compares the elements of this `Iterator` with those
|
|
0 commit comments