@@ -65,7 +65,7 @@ use default::Default;
65
65
use marker;
66
66
use mem;
67
67
use num:: { Int , Zero , One } ;
68
- use ops:: { self , Add , Sub , FnMut , RangeFrom } ;
68
+ use ops:: { self , Add , Sub , FnMut , Mul , RangeFrom } ;
69
69
use option:: Option :: { self , Some , None } ;
70
70
use marker:: Sized ;
71
71
use usize;
@@ -489,15 +489,14 @@ pub trait Iterator {
489
489
///
490
490
/// ```
491
491
/// # #![feature(core)]
492
- /// use std::iter::AdditiveIterator;
493
492
///
494
493
/// let a = [1, 4, 2, 3, 8, 9, 6];
495
- /// let sum = a.iter()
496
- /// .map(|x| *x)
497
- /// .inspect(|&x| println!("filtering {}", x))
498
- /// .filter(|&x| x % 2 == 0)
499
- /// .inspect(|&x| println!("{} made it through", x))
500
- /// .sum();
494
+ /// let sum: i32 = a.iter()
495
+ /// .map(|x| *x)
496
+ /// .inspect(|&x| println!("filtering {}", x))
497
+ /// .filter(|&x| x % 2 == 0)
498
+ /// .inspect(|&x| println!("{} made it through", x))
499
+ /// .sum();
501
500
/// println!("{}", sum);
502
501
/// ```
503
502
#[ inline]
@@ -1022,6 +1021,47 @@ pub trait Iterator {
1022
1021
}
1023
1022
}
1024
1023
}
1024
+
1025
+ /// Iterates over the entire iterator, summing up all the elements
1026
+ ///
1027
+ /// # Examples
1028
+ ///
1029
+ /// ```
1030
+ /// # #![feature(core)]
1031
+ ///
1032
+ /// let a = [1, 2, 3, 4, 5];
1033
+ /// let mut it = a.iter().cloned();
1034
+ /// assert!(it.sum::<i32>() == 15);
1035
+ /// ```
1036
+ #[ unstable( feature="core" ) ]
1037
+ fn sum < S =<Self as Iterator >:: Item > ( self ) -> S where
1038
+ S : Add < Self :: Item , Output =S > + Zero ,
1039
+ Self : Sized ,
1040
+ {
1041
+ self . fold ( Zero :: zero ( ) , |s, e| s + e)
1042
+ }
1043
+
1044
+ /// Iterates over the entire iterator, multiplying all the elements
1045
+ ///
1046
+ /// # Examples
1047
+ ///
1048
+ /// ```
1049
+ /// # #![feature(core)]
1050
+ ///
1051
+ /// fn factorial(n: u32) -> u32 {
1052
+ /// (1..).take_while(|&i| i <= n).product()
1053
+ /// }
1054
+ /// assert!(factorial(0) == 1);
1055
+ /// assert!(factorial(1) == 1);
1056
+ /// assert!(factorial(5) == 120);
1057
+ /// ```
1058
+ #[ unstable( feature="core" ) ]
1059
+ fn product < P =<Self as Iterator >:: Item > ( self ) -> P where
1060
+ P : Mul < Self :: Item , Output =P > + One ,
1061
+ Self : Sized ,
1062
+ {
1063
+ self . fold ( One :: one ( ) , |p, e| p * e)
1064
+ }
1025
1065
}
1026
1066
1027
1067
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -1222,95 +1262,6 @@ impl<I> RandomAccessIterator for Rev<I>
1222
1262
}
1223
1263
}
1224
1264
1225
- /// A trait for iterators over elements which can be added together
1226
- #[ unstable( feature = "core" ,
1227
- reason = "needs to be re-evaluated as part of numerics reform" ) ]
1228
- pub trait AdditiveIterator < A > {
1229
- /// Iterates over the entire iterator, summing up all the elements
1230
- ///
1231
- /// # Examples
1232
- ///
1233
- /// ```
1234
- /// # #![feature(core)]
1235
- /// use std::iter::AdditiveIterator;
1236
- ///
1237
- /// let a = [1, 2, 3, 4, 5];
1238
- /// let mut it = a.iter().cloned();
1239
- /// assert!(it.sum() == 15);
1240
- /// ```
1241
- fn sum ( self ) -> A ;
1242
- }
1243
-
1244
- macro_rules! impl_additive {
1245
- ( $A: ty, $init: expr) => {
1246
- #[ unstable( feature = "core" , reason = "trait is experimental" ) ]
1247
- impl <T : Iterator <Item =$A>> AdditiveIterator <$A> for T {
1248
- #[ inline]
1249
- fn sum( self ) -> $A {
1250
- self . fold( $init, |acc, x| acc + x)
1251
- }
1252
- }
1253
- } ;
1254
- }
1255
- impl_additive ! { i8 , 0 }
1256
- impl_additive ! { i16 , 0 }
1257
- impl_additive ! { i32 , 0 }
1258
- impl_additive ! { i64 , 0 }
1259
- impl_additive ! { isize , 0 }
1260
- impl_additive ! { u8 , 0 }
1261
- impl_additive ! { u16 , 0 }
1262
- impl_additive ! { u32 , 0 }
1263
- impl_additive ! { u64 , 0 }
1264
- impl_additive ! { usize , 0 }
1265
- impl_additive ! { f32 , 0.0 }
1266
- impl_additive ! { f64 , 0.0 }
1267
-
1268
- /// A trait for iterators over elements which can be multiplied together.
1269
- #[ unstable( feature = "core" ,
1270
- reason = "needs to be re-evaluated as part of numerics reform" ) ]
1271
- pub trait MultiplicativeIterator < A > {
1272
- /// Iterates over the entire iterator, multiplying all the elements
1273
- ///
1274
- /// # Examples
1275
- ///
1276
- /// ```
1277
- /// # #![feature(core)]
1278
- /// use std::iter::MultiplicativeIterator;
1279
- ///
1280
- /// fn factorial(n: usize) -> usize {
1281
- /// (1..).take_while(|&i| i <= n).product()
1282
- /// }
1283
- /// assert!(factorial(0) == 1);
1284
- /// assert!(factorial(1) == 1);
1285
- /// assert!(factorial(5) == 120);
1286
- /// ```
1287
- fn product ( self ) -> A ;
1288
- }
1289
-
1290
- macro_rules! impl_multiplicative {
1291
- ( $A: ty, $init: expr) => {
1292
- #[ unstable( feature = "core" , reason = "trait is experimental" ) ]
1293
- impl <T : Iterator <Item =$A>> MultiplicativeIterator <$A> for T {
1294
- #[ inline]
1295
- fn product( self ) -> $A {
1296
- self . fold( $init, |acc, x| acc * x)
1297
- }
1298
- }
1299
- } ;
1300
- }
1301
- impl_multiplicative ! { i8 , 1 }
1302
- impl_multiplicative ! { i16 , 1 }
1303
- impl_multiplicative ! { i32 , 1 }
1304
- impl_multiplicative ! { i64 , 1 }
1305
- impl_multiplicative ! { isize , 1 }
1306
- impl_multiplicative ! { u8 , 1 }
1307
- impl_multiplicative ! { u16 , 1 }
1308
- impl_multiplicative ! { u32 , 1 }
1309
- impl_multiplicative ! { u64 , 1 }
1310
- impl_multiplicative ! { usize , 1 }
1311
- impl_multiplicative ! { f32 , 1.0 }
1312
- impl_multiplicative ! { f64 , 1.0 }
1313
-
1314
1265
/// `MinMaxResult` is an enum returned by `min_max`. See `Iterator::min_max` for
1315
1266
/// more detail.
1316
1267
#[ derive( Clone , PartialEq , Debug ) ]
0 commit comments