Skip to content

Commit 80e309f

Browse files
authored
Rollup merge of #99244 - gthb:doc-improve-iterator-scan, r=m-ou-se
doc: clearer and more correct Iterator::scan The `Iterator::scan` documentation seemed a little misleading to my newcomer eyes, and this tries to address that. * I found “similar to `fold`” unhelpful because (a) the similarity is only that they maintain state between iterations, and (b) the _dissimilarity_ is no less important: one returns a final value and the other an iterator. So this replaces that with “which, like `fold`, holds internal state, but unlike `fold`, produces a new iterator. * I found “the return value from the closure, an `Option`, is yielded by the iterator” to be downright incorrect, because “yielded by the iterator” means “returned by the `next` method wrapped in `Some`”, so this implied that `scan` would convert an input iterator of `T` to an output iterator of `Option<T>`. So this replaces “yielded by the iterator” with “returned by the `next` method” and elaborates: “Thus the closure can return `Some(value)` to yield `value`, or `None` to end the iteration.” * This also changes the example to illustrate the latter point by returning `None` to terminate the iteration early based on `state`.
2 parents f6cc345 + 588592b commit 80e309f

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

library/core/src/iter/traits/iterator.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -1381,8 +1381,8 @@ pub trait Iterator {
13811381
Take::new(self, n)
13821382
}
13831383

1384-
/// An iterator adapter similar to [`fold`] that holds internal state and
1385-
/// produces a new iterator.
1384+
/// An iterator adapter which, like [`fold`], holds internal state, but
1385+
/// unlike [`fold`], produces a new iterator.
13861386
///
13871387
/// [`fold`]: Iterator::fold
13881388
///
@@ -1394,20 +1394,25 @@ pub trait Iterator {
13941394
///
13951395
/// On iteration, the closure will be applied to each element of the
13961396
/// iterator and the return value from the closure, an [`Option`], is
1397-
/// yielded by the iterator.
1397+
/// returned by the `next` method. Thus the closure can return
1398+
/// `Some(value)` to yield `value`, or `None` to end the iteration.
13981399
///
13991400
/// # Examples
14001401
///
14011402
/// Basic usage:
14021403
///
14031404
/// ```
1404-
/// let a = [1, 2, 3];
1405+
/// let a = [1, 2, 3, 4];
14051406
///
14061407
/// let mut iter = a.iter().scan(1, |state, &x| {
1407-
/// // each iteration, we'll multiply the state by the element
1408+
/// // each iteration, we'll multiply the state by the element ...
14081409
/// *state = *state * x;
14091410
///
1410-
/// // then, we'll yield the negation of the state
1411+
/// // ... and terminate if the state exceeds 6
1412+
/// if *state > 6 {
1413+
/// return None;
1414+
/// }
1415+
/// // ... else yield the negation of the state
14111416
/// Some(-*state)
14121417
/// });
14131418
///

0 commit comments

Comments
 (0)