@@ -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,56 @@ pub trait Itertools: Iterator {
2223
2220
self . collect ( )
2224
2221
}
2225
2222
2223
+ /// `.product_ok()` is a more convenient way of writing `.product::<Result<_, _>>()`
2224
+ ///
2225
+ /// **Panics** when a primitive integer type is returned and the computation
2226
+ /// overflows, and debug assertions are enabled.
2227
+ ///
2228
+ /// # Example
2229
+ ///
2230
+ /// ```
2231
+ /// use itertools::Itertools;
2232
+ /// use std::str::FromStr;
2233
+ ///
2234
+ /// fn main() -> Result<(), std::num::ParseIntError> {
2235
+ /// let product: u64 = ["1", "2", "3"].iter().map(|x| u64::from_str(x)).product_ok()?;
2236
+ /// assert_eq!(product, 6);
2237
+ /// Ok(())
2238
+ /// }
2239
+ /// ```
2240
+ fn product_ok < T , U , E > ( self ) -> Result < U , E >
2241
+ where
2242
+ Self : Sized + Iterator < Item = Result < T , E > > ,
2243
+ Result < U , E > : std:: iter:: Product < Result < T , E > > ,
2244
+ {
2245
+ self . product ( )
2246
+ }
2247
+
2248
+ /// `.sum_ok()` is a more convenient way of writing `.sum::<Result<_, _>>()`
2249
+ ///
2250
+ /// **Panics** when a primitive integer type is returned and the computation
2251
+ /// overflows, and debug assertions are enabled.
2252
+ ///
2253
+ /// # Example
2254
+ ///
2255
+ /// ```
2256
+ /// use itertools::Itertools;
2257
+ /// use std::str::FromStr;
2258
+ ///
2259
+ /// fn main() -> Result<(), std::num::ParseIntError> {
2260
+ /// let sum: u64 = ["1", "2", "3"].iter().map(|x| u64::from_str(x)).sum_ok()?;
2261
+ /// assert_eq!(sum, 6);
2262
+ /// Ok(())
2263
+ /// }
2264
+ /// ```
2265
+ fn sum_ok < T , U , E > ( self ) -> Result < U , E >
2266
+ where
2267
+ Self : Sized + Iterator < Item = Result < T , E > > ,
2268
+ Result < U , E > : std:: iter:: Sum < Result < T , E > > ,
2269
+ {
2270
+ self . sum ( )
2271
+ }
2272
+
2226
2273
/// Assign to each reference in `self` from the `from` iterator,
2227
2274
/// stopping at the shortest of the two iterators.
2228
2275
///
0 commit comments