Skip to content

Commit 6867806

Browse files
committed
Document workings of successors more clearly
This is an attempt to fix #135087 together with #135886, but I am not sure if I've succeeded in adding much clarity here, so don't be shy with your comments.
1 parent dee7d0e commit 6867806

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

library/core/src/iter/sources/successors.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
use crate::fmt;
22
use crate::iter::FusedIterator;
33

4-
/// Creates a new iterator where each successive item is computed based on the preceding one.
4+
/// Creates an iterator which, starting from an initial item,
5+
/// computes each successive item from the preceding one.
56
///
6-
/// The iterator starts with the given first item (if any)
7-
/// and calls the given `FnMut(&T) -> Option<T>` closure to compute each item’s successor.
8-
/// The iterator will yield the `T`s returned from the closure.
7+
/// This iterator stores an optional item (`Option<T>`) and a successor closure (`impl FnMut(&T) -> Option<T>`).
8+
/// Its `next` method returns the stored optional item and
9+
/// if it is `Some(val)` calls the stored closure on `&val` to compute and store its successor.
10+
/// The iterator will apply the closure successively to the stored option's value until the option is `None`.
11+
/// This also means that once the stored option is `None` it will remain `None`,
12+
/// as the closure will not be called again, so the created iterator is a [`FusedIterator`].
13+
/// The iterator's items will be the initial item and all of its successors as calculated by the successor closure.
914
///
1015
/// ```
1116
/// use std::iter::successors;
@@ -24,7 +29,8 @@ where
2429
Successors { next: first, succ }
2530
}
2631

27-
/// A new iterator where each successive item is computed based on the preceding one.
32+
/// An iterator which, starting from an initial item,
33+
/// computes each successive item from the preceding one.
2834
///
2935
/// This `struct` is created by the [`iter::successors()`] function.
3036
/// See its documentation for more.

0 commit comments

Comments
 (0)