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§

source

fn collect_and_apply<I, F>(iter: I, f: F) -> Self::Outputwhere I: Iterator<Item = Self>, F: FnOnce(&[T]) -> R,

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.

Implementors§