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§


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.

Implementations on Foreign Types§


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.


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

Equivalent to Ok(f(&iter.collect::<Result<Vec<_>>>()?)).


type Output = Result<R, E>



impl<T, R> CollectAndApply<T, R> for T

The blanket impl that always collects all elements and applies f.


type Output = R