Trait CollectAndApply
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§
type Output
Required Methods§
fn 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.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementations on Foreign Types§
§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.
Implementors§
§impl<T, R> CollectAndApply<T, R> for T
impl<T, R> CollectAndApply<T, R> for T
The blanket impl that always collects all elements and applies f
.