IteratorExt

Trait IteratorExt 

Source
pub trait IteratorExt: Iterator {
    // Provided methods
    unsafe fn collect_nonempty_unchecked(self) -> NonEmptyVec<Self::Item>
       where Self: Sized { ... }
    fn collect_nonempty(self) -> Result<NonEmptyVec<Self::Item>, EmptyError>
       where Self: Sized { ... }
}
Expand description

Extends Iterator with methods for collecting NonEmptyVecs as a stop-gap to retain a fallible Iterator::collect functionality.

Unlike Iterator::collect, the use of the turbo-fish operator is likely not required as the method only collects into a NonEmptyVec.

Provided Methods§

Source

unsafe fn collect_nonempty_unchecked(self) -> NonEmptyVec<Self::Item>
where Self: Sized,

Available on (crate features alloc or std) and non-no_global_oom_handling only.

Transforms an iterator into a NonEmptyVec<Self::Item> without validating if the iterator has items.

If you’re not certain that the iterator won’t be empty, use IteratorExt::collect_nonempty instead.

§Safety

The iterator must not be empty.

§Examples
use not_empty::{IteratorExt, NonEmptyVec};

let array = [1, 2, 3];

unsafe {
    let doubled = array
        .iter()
        .map(|&x| x * 2)
        .collect_nonempty_unchecked();
    assert_eq!(doubled, not_empty::vec![2, 4, 6]);
}
Source

fn collect_nonempty(self) -> Result<NonEmptyVec<Self::Item>, EmptyError>
where Self: Sized,

Available on (crate features alloc or std) and non-no_global_oom_handling only.

Transforms an iterator into a NonEmptyVec<Self::Item>.

§Errors

Returns an EmptyError if the iterator yields no values.

§Examples
use not_empty::IteratorExt;

let array = [1, 2, 3];
let doubled = array.iter().map(|&x| x * 2).collect_nonempty()?;
assert_eq!(doubled, not_empty::vec![2, 4, 6]);

Implementors§

Source§

impl<I> IteratorExt for I
where I: Iterator,