-
Notifications
You must be signed in to change notification settings - Fork 13k
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
core::iter::from_fn
and core::iter::successors
documentation does not explain callback function signature
#135087
Comments
Yeah... Perhaps, core::iter::from_fnCreates a new iterator where each iteration calls the provided closure [...] core::iter::successorsCreates a new iterator where each successive item is computed based on the preceding one. The iterator starts with the given first item (if any) and calls the given [...] |
…s, r=jhpratt Clarified the documentation on `core::iter::from_fn` and `core::iter::successors` This PR clarifies the closure requirements for `core::iter::from_fn` and `core::iter::successors`. `std::iter::successors` in particular is a bit difficult to understand if you are not already familiar with the signature of [`checked_mul`](https://docs.rs/num/latest/num/trait.CheckedMul.html) used in the example. See rust-lang#135087
Rollup merge of rust-lang#135118 - ranger-ross:better-docs-on-iter-fns, r=jhpratt Clarified the documentation on `core::iter::from_fn` and `core::iter::successors` This PR clarifies the closure requirements for `core::iter::from_fn` and `core::iter::successors`. `std::iter::successors` in particular is a bit difficult to understand if you are not already familiar with the signature of [`checked_mul`](https://docs.rs/num/latest/num/trait.CheckedMul.html) used in the example. See rust-lang#135087
Note that the iterator can resume after E.g. this works fn main() {
let mut i = 0;
let mut iter = std::iter::from_fn(move || {
i += 1;
if i % 3 == 0 {
None
} else {
Some(i)
}
});
assert_eq!((&mut iter).collect::<Vec<_>>(), [1,2]);
assert_eq!(iter.next(), Some(4));
assert_eq!(iter.next(), Some(5));
assert_eq!(iter.next(), None);
} |
Yeah, that was brought up in #135118 here 👉 #135118 (comment) I ended up dropping the "until it returns |
@rick-de-water would it help to document that this function argument is essentially the returned iterators |
@hkBst Yes, that would work for |
This is an attempt to fix rust-lang#135087 together with rust-lang#135886, but I am not sure if I've succeeded in adding much clarity here, so don't be shy with your comments.
Document purpose of closure in from_fn.rs more clearly partial fix for rust-lang#135087 together with rust-lang#135895
Location
core::iter::from_fn
core::iter::successors
Summary
Both of these functions take callback methods that return an
Option<T>
. However, nowhere is actually described that thisOption<T>
is used to detect the end of the iterator (i.e. returnSome
while there are items, returnNone
when at the end). I was able to deduce how things worked eventually by looking at the examples, but I was definitely a bit confused at first.There should be a clear description on what the callback is expected to return, not just when the callback is called.
The text was updated successfully, but these errors were encountered: