@@ -1338,11 +1338,11 @@ pub trait Iterator {
1338
1338
/// An iterator method that applies a function as long as it returns
1339
1339
/// successfully, producing a single, final value.
1340
1340
///
1341
- /// `fold ()` takes two arguments: an initial value, and a closure with two
1342
- /// arguments: an 'accumulator', and an element. The closure either returns
1343
- /// successfully, with the value that the accumulator should have for the
1344
- /// next iteration, or it returns failure, with an error value that is
1345
- /// propagated back to the caller immediately (short-circuiting).
1341
+ /// `try_fold ()` takes two arguments: an initial value, and a closure with
1342
+ /// two arguments: an 'accumulator', and an element. The closure either
1343
+ /// returns successfully, with the value that the accumulator should have
1344
+ /// for the next iteration, or it returns failure, with an error value that
1345
+ /// is propagated back to the caller immediately (short-circuiting).
1346
1346
///
1347
1347
/// The initial value is the value the accumulator will have on the first
1348
1348
/// call. If applying the closure succeeded against every element of the
@@ -1353,9 +1353,16 @@ pub trait Iterator {
1353
1353
///
1354
1354
/// # Note to Implementors
1355
1355
///
1356
- /// If possible, override this method with an implementation using
1357
- /// internal iteration. Most of the other methods have their default
1358
- /// implementation in terms of this one.
1356
+ /// Most of the other (forward) methods have default implementations in
1357
+ /// terms of this one, so try to implement this explicitly if it can
1358
+ /// do something better than the default `for` loop implementation.
1359
+ ///
1360
+ /// In particular, try to have this call `try_fold()` on the internal parts
1361
+ /// from which this iterator is composed. If multiple calls are needed,
1362
+ /// the `?` operator be convenient for chaining the accumulator value along,
1363
+ /// but beware any invariants that need to be upheld before those early
1364
+ /// returns. This is a `&mut self` method, so iteration needs to be
1365
+ /// resumable after hitting an error here.
1359
1366
///
1360
1367
/// # Examples
1361
1368
///
@@ -1687,7 +1694,7 @@ pub trait Iterator {
1687
1694
// The addition might panic on overflow
1688
1695
self . try_fold ( 0 , move |i, x| {
1689
1696
if predicate ( x) { SearchResult :: Found ( i) }
1690
- else { SearchResult :: NotFound ( i+ 1 ) }
1697
+ else { SearchResult :: NotFound ( i + 1 ) }
1691
1698
} ) . into_option ( )
1692
1699
}
1693
1700
@@ -1973,7 +1980,7 @@ pub trait Iterator {
1973
1980
let mut ts: FromA = Default :: default ( ) ;
1974
1981
let mut us: FromB = Default :: default ( ) ;
1975
1982
1976
- self . for_each ( |( t, u) |{
1983
+ self . for_each ( |( t, u) | {
1977
1984
ts. extend ( Some ( t) ) ;
1978
1985
us. extend ( Some ( u) ) ;
1979
1986
} ) ;
@@ -2380,7 +2387,7 @@ trait SpecIterator : Iterator {
2380
2387
fn spec_nth ( & mut self , n : usize ) -> Option < Self :: Item > ;
2381
2388
}
2382
2389
2383
- impl < I : Iterator + ?Sized > SpecIterator for I {
2390
+ impl < I : Iterator + ?Sized > SpecIterator for I {
2384
2391
default fn spec_nth ( & mut self , mut n : usize ) -> Option < Self :: Item > {
2385
2392
for x in self {
2386
2393
if n == 0 { return Some ( x) }
@@ -2390,11 +2397,11 @@ impl<I:Iterator+?Sized> SpecIterator for I {
2390
2397
}
2391
2398
}
2392
2399
2393
- impl < I : Iterator + Sized > SpecIterator for I {
2400
+ impl < I : Iterator + Sized > SpecIterator for I {
2394
2401
fn spec_nth ( & mut self , n : usize ) -> Option < Self :: Item > {
2395
2402
self . try_fold ( n, move |i, x| {
2396
2403
if i == 0 { SearchResult :: Found ( x) }
2397
- else { SearchResult :: NotFound ( i- 1 ) }
2404
+ else { SearchResult :: NotFound ( i - 1 ) }
2398
2405
} ) . into_option ( )
2399
2406
}
2400
2407
}
0 commit comments