Function lending_iterator::lending_iterator::constructors::from_fn
source · [−]pub fn from_fn<Item, State, Next>(
state: State,
next: Next
) -> FromFn<Item, State, Next>ⓘNotable traits for FromFn<Item, State, Next>impl<'next, Item, State, Next> LendingIteratorඞItem<'next, &'next FromFn<Item, State, Next>> for FromFn<Item, State, Next> where
Item: HKT,
Next: FnMut(&mut State) -> Option<Feed<'_, Item>>, type T = Feed<'next, Item>;
where
Item: HKT,
Next: FnMut(&mut State) -> Option<Feed<'_, Item>>,
Expand description
Main ad-hoc / closure-based constructor of LendingIterator
s.
It expects the both necessary and sufficient elements for such an impl:
-
a
State
, which will play a role akin to that ofSelf
in a manual impl; -
a
fn next
“method” on it. There is actually a certain level of flexibility gained from this being a closure rather than a stateless associated function.For instance, non-lent state (such as an internal mutable counter) can be implicity captured by such a closure, without having to funnel it through the lendable
State
.
It can also be viewed as a convenience layer over:
::lending_iterator::repeat_mut(state)
.filter_map::<Item, _>(move |[], it| next(it))
The returned struct
can also be used directly, to benefit from “named
arguments”, at the cost of having to provide a PhantomData
parameter.