pub trait IteratorExt: Iterator {
    fn extending<C>(self, extend: &mut C)
    where
        Self: Sized,
        C: Extend<Self::Item>
, { ... } fn collect_into<C>(self, extend: C) -> C
    where
        Self: Sized,
        C: Extend<Self::Item>
, { ... } fn replace_nth(self, nth: usize, with: Self::Item) -> ReplaceNth<Self>Notable traits for ReplaceNth<I>impl<I> Iterator for ReplaceNth<I> where
    I: Iterator
type Item = I::Item;

    where
        Self: Sized
, { ... } fn sum_same(self) -> Self::Item
    where
        Self: Sized,
        Self::Item: Sum
, { ... } fn product_same(self) -> Self::Item
    where
        Self: Sized,
        Self::Item: Product
, { ... } }
Available on crate feature iterators only.
Expand description

Extension trait for std::iter::Iterator implementors.

Provided Methods

Collects into an existing collection by extending it.

Example
use core_extensions::iterators::IteratorExt;

let mut list = vec![101, 102];

(0..10)
    .filter(|&v| v<5 )
    .map(|v| v*2 )
    .extending(&mut list);

assert_eq!(list, vec![101, 102, 0, 2, 4, 6, 8]);

Collects into a pre-allocated collection,returning it by value.

Example
use core_extensions::iterators::IteratorExt;

let list = (0..10)
    .filter(|&v| v<5 )
    .map(|v| v*2 )
    .collect_into(Vec::with_capacity(5));

assert_eq!(list.capacity(), 5);
assert_eq!(list, vec![0, 2, 4, 6, 8]);
Example

Reusing an existing collection.

use core_extensions::iterators::IteratorExt;

let mut list = Vec::with_capacity(7);
list.push(100);
list.push(101);

let list = (0..10)
    .filter(|&v| v<5 )
    .map(|v| v*2 )
    .collect_into(list);

assert_eq!(list.capacity(),7);
assert_eq!(list, vec![100, 101, 0, 2, 4, 6, 8]);

An Iterator that replaces the nth element with another value.

Example
use core_extensions::iterators::IteratorExt;

assert_eq!(
    (0..=9).replace_nth(5, 1337).collect::<Vec<_>>(),
    vec![0, 1, 2, 3, 4, 1337, 6, 7, 8, 9]
);

let list = vec!["hello", "dear", "world"];

assert_eq!(
    list.into_iter().replace_nth(1, "my").collect::<Vec<_>>(),
    vec!["hello", "my", "world"]
);

Sums the items of the iterator, into the item’s type.

This like the Iterator::sum method, with better type inference, since with the Iterator::sum method you must specify its return type.

Example
use core_extensions::iterators::IteratorExt;

assert_eq!((1..=4).sum_same(), 10);
 
let arr = [3, 7, 11, 29];
assert_eq!(arr.iter().copied().sum_same(), 50);
 

Multiplies the items of the iterator, into the item’s type.

This like the Iterator::product method, with better type inference, since with the Iterator::product method you must specify its return type.

Example
use core_extensions::iterators::IteratorExt;

assert_eq!((1..=4).product_same(), 24);
 
let arr = [3, 4, 6];
assert_eq!(arr.iter().copied().product_same(), 72);
 

Implementors