pub fn from_fn<T, F>(f: F) -> FromFn<F> ⓘ
Expand description
Creates a new iterator where each iteration calls the provided closure
F: FnMut() -> Option<T>
.
This allows creating a custom iterator with any behavior
without using the more verbose syntax of creating a dedicated type
and implementing the Iterator
trait for it.
Note that the FromFn
iterator doesn’t make assumptions about the behavior of the closure,
and therefore conservatively does not implement FusedIterator
,
or override Iterator::size_hint()
from its default (0, None)
.
The closure can use captures and its environment to track state across iterations. Depending on
how the iterator is used, this may require specifying the move
keyword on the closure.
§Examples
Let’s re-implement the counter iterator from module-level documentation: