Skip to content

doc: clearer and more correct Iterator::scan #99244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions library/core/src/iter/traits/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1380,8 +1380,8 @@ pub trait Iterator {
Take::new(self, n)
}

/// An iterator adapter similar to [`fold`] that holds internal state and
/// produces a new iterator.
/// An iterator adapter which, like [`fold`], holds internal state, but
/// unlike [`fold`], produces a new iterator.
///
/// [`fold`]: Iterator::fold
///
Expand All @@ -1393,20 +1393,25 @@ pub trait Iterator {
///
/// On iteration, the closure will be applied to each element of the
/// iterator and the return value from the closure, an [`Option`], is
/// yielded by the iterator.
/// returned by the `next` method. Thus the closure can return
/// `Some(value)` to yield `value`, or `None` to end the iteration.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// let a = [1, 2, 3];
/// let a = [1, 2, 3, 4];
///
/// let mut iter = a.iter().scan(1, |state, &x| {
/// // each iteration, we'll multiply the state by the element
/// // each iteration, we'll multiply the state by the element ...
/// *state = *state * x;
///
/// // then, we'll yield the negation of the state
/// // ... and terminate if the state exceeds 6
/// if *state > 6 {
/// return None;
/// }
/// // ... else yield the negation of the state
/// Some(-*state)
/// });
///
Expand Down