@@ -52,10 +52,8 @@ extern crate alloc;
52
52
53
53
#[ cfg( feature = "use_alloc" ) ]
54
54
use alloc:: { string:: String , vec:: Vec } ;
55
-
56
- pub use either:: Either ;
57
-
58
55
use core:: borrow:: Borrow ;
56
+ pub use either:: Either ;
59
57
use std:: cmp:: Ordering ;
60
58
#[ cfg( feature = "use_std" ) ]
61
59
use std:: collections:: HashMap ;
@@ -144,8 +142,7 @@ pub mod traits {
144
142
145
143
pub use crate :: concat_impl:: concat;
146
144
pub use crate :: cons_tuples_impl:: cons_tuples;
147
- pub use crate :: diff:: diff_with;
148
- pub use crate :: diff:: Diff ;
145
+ pub use crate :: diff:: { diff_with, Diff } ;
149
146
#[ cfg( feature = "use_alloc" ) ]
150
147
pub use crate :: kmerge_impl:: kmerge_by;
151
148
pub use crate :: minmax:: MinMaxResult ;
@@ -2195,7 +2192,7 @@ pub trait Itertools: Iterator {
2195
2192
self . collect ( )
2196
2193
}
2197
2194
2198
- /// `.try_collect()` is more convenient way of writing
2195
+ /// `.try_collect()` is a more convenient way of writing
2199
2196
/// `.collect::<Result<_, _>>()`
2200
2197
///
2201
2198
/// # Example
@@ -2223,6 +2220,50 @@ pub trait Itertools: Iterator {
2223
2220
self . collect ( )
2224
2221
}
2225
2222
2223
+ /// `.try_product()` is a more convenient way of writing `.product::<Result<_, _>>()`
2224
+ ///
2225
+ /// # Example
2226
+ ///
2227
+ /// ```
2228
+ /// use itertools::Itertools;
2229
+ /// use std::str::FromStr;
2230
+ ///
2231
+ /// fn main() -> Result<(), std::num::ParseIntError> {
2232
+ /// let product: u64 = ["1", "2", "3"].iter().map(|x| u64::from_str(x)).try_product()?;
2233
+ /// assert_eq!(product, 6);
2234
+ /// Ok(())
2235
+ /// }
2236
+ /// ```
2237
+ fn try_product < T , U , E > ( self ) -> Result < U , E >
2238
+ where
2239
+ Self : Sized + Iterator < Item = Result < T , E > > ,
2240
+ Result < U , E > : std:: iter:: Product < Result < T , E > > ,
2241
+ {
2242
+ self . product ( )
2243
+ }
2244
+
2245
+ /// `.try_sum()` is a more convenient way of writing `.sum::<Result<_, _>>()`
2246
+ ///
2247
+ /// # Example
2248
+ ///
2249
+ /// ```
2250
+ /// use itertools::Itertools;
2251
+ /// use std::str::FromStr;
2252
+ ///
2253
+ /// fn main() -> Result<(), std::num::ParseIntError> {
2254
+ /// let sum: u64 = ["1", "2", "3"].iter().map(|x| u64::from_str(x)).try_sum()?;
2255
+ /// assert_eq!(sum, 6);
2256
+ /// Ok(())
2257
+ /// }
2258
+ /// ```
2259
+ fn try_sum < T , U , E > ( self ) -> Result < U , E >
2260
+ where
2261
+ Self : Sized + Iterator < Item = Result < T , E > > ,
2262
+ Result < U , E > : std:: iter:: Sum < Result < T , E > > ,
2263
+ {
2264
+ self . sum ( )
2265
+ }
2266
+
2226
2267
/// Assign to each reference in `self` from the `from` iterator,
2227
2268
/// stopping at the shortest of the two iterators.
2228
2269
///
0 commit comments