core::iter::Unfold: reword docs and add example #19512
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Added the example from this Reddit thread, reworked to be more robust with correct logic (first link skipped the 0th and 1st Fibonacci numbers, second forgot about the last two valid values before overflow). Will yield all Fibonacci numbers sequentially in the range
[0, <u32 as Int>::max_value())
.If the example is too complicated I can change it to a more naive version, perhaps using signed integers to check for overflow instead of
Option
and.checked_add()
.Also reworded the doc comments to clarify the usage and behavior of
Unfold
, as the thread suggested that it wasn't really clear howUnfold
worked and when one should use it.This change is in the
core
crate but I based the example onstd
since that's where most readers will find the example. I included a note aboutcore
for clarity. Edit: removed.Tested with
rustdoc src/libcore/lib.rs
. Rebased against latest master as of the creation of this PR.