Open matj1 opened 2 days ago
Does core::iter::from_fn
meet your requirements?
It does almost what I want, but I dislike that it needs an external variable to track the state. I prefer a single expression to create the iterator. If I enclose the variable with the call to from_fn
in a scope to have it as a single expression, I get an error that the variable does not live long enough.
I want a way to easily create a finite iterator from an initial state and a function creating the next state, but, unlike the present function
iterate
, it takes a function which gives the new state already wrapped inOption
, which allows the function to end the iterator. The result ofiterate
will never end.I use this implementation based on
iterate
, and I could commit it to this repository, but I would like to discuss how it should be named and if it could be done better.