TryCollect

Trait TryCollect 

Source
pub trait TryCollect<T> {
    // Required methods
    fn try_collect_in<A: Allocator>(
        self,
        alloc: A,
    ) -> Result<Vec<T, A>, TryReserveError>;
    fn try_collect(self) -> Result<Vec<T>, TryReserveError>;
}
Expand description

Fallible allocations equivalents for Iterator::collect.

Required Methods§

Source

fn try_collect_in<A: Allocator>( self, alloc: A, ) -> Result<Vec<T, A>, TryReserveError>

Attempts to collect items from an iterator into a vector with the provided allocator.

§Examples
use fallible_vec::*;
use std::alloc::System;

let doubled = [1, 2, 3, 4, 5].map(|i| i * 2);
let vec = doubled.try_collect_in(System)?;
assert_eq!(vec, [2, 4, 6, 8, 10]);
Source

fn try_collect(self) -> Result<Vec<T>, TryReserveError>

Attempts to collect items from an iterator into a vector.

§Examples
use fallible_vec::*;

let doubled = [1, 2, 3, 4, 5].map(|i| i * 2);
let vec = doubled.try_collect()?;
assert_eq!(vec, [2, 4, 6, 8, 10]);

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.

Implementors§

Source§

impl<T, I> TryCollect<T> for I
where I: IntoIterator<Item = T>,