pub trait CollectAndApply<T, R>: Sized {
type Output;
// Required method
fn collect_and_apply<I, F>(iter: I, f: F) -> Self::Output
where I: Iterator<Item = Self>,
F: FnOnce(&[T]) -> R;
}
Expand description
Imagine you have a function F: FnOnce(&[T]) -> R
, plus an iterator iter
that produces T
items. You could combine them with
f(&iter.collect::<Vec<_>>())
, but this requires allocating memory for the
Vec
.
This trait allows for faster implementations, intended for cases where the
number of items produced by the iterator is small. There is a blanket impl
for T
items, but there is also a fallible impl for Result<T, E>
items.
Required Associated Types§
Required Methods§
sourcefn collect_and_apply<I, F>(iter: I, f: F) -> Self::Output
fn collect_and_apply<I, F>(iter: I, f: F) -> Self::Output
Produce a result of type Self::Output
from iter
. The result will
typically be produced by applying f
on the elements produced by
iter
, though this may not happen in some impls, e.g. if an error
occurred during iteration.
Object Safety§
This trait is not object safe.
Implementations on Foreign Types§
source§impl<T, R, E> CollectAndApply<T, R> for Result<T, E>
impl<T, R, E> CollectAndApply<T, R> for Result<T, E>
A fallible impl that will fail, without calling f
, if there are any
errors during collection.