}
}
}
+
+ /// Iterates over the entire iterator, summing up all the elements
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// # #![feature(core)]
+ ///
+ /// let a = [1, 2, 3, 4, 5];
+ /// let mut it = a.iter().cloned();
+ /// assert!(it.sum::<i32>() == 15);
+ /// ```
+ #[unstable(feature="core")]
+ fn sum<S=<Self as Iterator>::Item>(self) -> S where
+ S: Add<Self::Item, Output=S> + Zero,
+ Self: Sized,
+ {
+ self.fold(Zero::zero(), |s, e| s + e)
+ }
+
+ /// Iterates over the entire iterator, multiplying all the elements
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// # #![feature(core)]
+ ///
+ /// fn factorial(n: u32) -> u32 {
+ /// (1..).take_while(|&i| i <= n).product()
+ /// }
+ /// assert!(factorial(0) == 1);
+ /// assert!(factorial(1) == 1);
+ /// assert!(factorial(5) == 120);
+ /// ```
+ #[unstable(feature="core")]
+ fn product<P=<Self as Iterator>::Item>(self) -> P where
+ P: Mul<Self::Item, Output=P> + One,
+ Self: Sized,
+ {
+ self.fold(One::one(), |p, e| p * e)
+ }
}
+ /// Select an element from an iterator based on the given projection
+ /// and "comparison" function.
+ ///
+ /// This is an idiosyncratic helper to try to factor out the
+ /// commonalities of {max,min}{,_by}. In particular, this avoids
+ /// having to implement optimisations several times.
+ #[inline]
+ fn select_fold1<I,B, FProj, FCmp>(mut it: I,
+ mut f_proj: FProj,
+ mut f_cmp: FCmp) -> Option<(B, I::Item)>
+ where I: Iterator,
+ FProj: FnMut(&I::Item) -> B,
+ FCmp: FnMut(&B, &I::Item, &B, &I::Item) -> bool
+ {
+ // start with the first element as our selection. This avoids
+ // having to use `Option`s inside the loop, translating to a
+ // sizeable performance gain (6x in one case).
+ it.next().map(|mut sel| {
+ let mut sel_p = f_proj(&sel);
+
+ for x in it {
+ let x_p = f_proj(&x);
+ if f_cmp(&sel_p, &sel, &x_p, &x) {
+ sel = x;
+ sel_p = x_p;
+ }
+ }
+ (sel_p, sel)
+ })
+ }
+
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, I: Iterator + ?Sized> Iterator for &'a mut I {
type Item = I::Item;