[][src]Trait core_extensions::iterators::IteratorExt

pub trait IteratorExt: Iterator {
    fn collect_<T>(self, _: VariantPhantom<T>) -> T
    where
        Self: Sized,
        T: FromIterator<Self::Item>
, { ... }
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>
    where
        Self: Sized
, { ... } }

Extension trait for Iterator implementors.

Provided methods

fn collect_<T>(self, _: VariantPhantom<T>) -> T where
    Self: Sized,
    T: FromIterator<Self::Item>, 

Alternative to Iterator::collect .

This method is defined to allow using the .collect_(type::T) syntax.

type::T is an associated constant defined for every type here.

Example ,collecting into a Vec

use core_extensions::iterators::IteratorExt;
use core_extensions::SelfOps;

assert_eq!(
    (0..100).collect_(Vec::T),
    (0..100).collect::<Vec<_>>()
);

Example,collecting into a Vec specifying the type parameter.

use core_extensions::iterators::IteratorExt;
use core_extensions::SelfOps;

assert_eq!(
    (0..100).collect_(Vec::<usize>::T),
    (0..100).collect::<Vec<usize>>()
);

Example of a limitation of collect_

Because of type parameter defaults,it's necessary to create a type alias to convince Rust to instantiate HashMap<K,V,S> with the default type for S.

use core_extensions::iterators::IteratorExt;
use core_extensions::SelfOps;
use std::collections::HashMap;

type Map<K,V>=HashMap<K,V>;

let list=vec![("hello",10),("world",20)];

let map=list.iter().cloned().collect_(Map::T);

assert_eq!(map["hello"],10);
assert_eq!(map["world"],20);

fn extending<C>(self, extend: &mut C) where
    Self: Sized,
    C: Extend<Self::Item>, 

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]);

fn collect_into<C>(self, extend: C) -> C where
    Self: Sized,
    C: Extend<Self::Item>, 

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]);

Important traits for ReplaceNth<I>
fn replace_nth(self, nth: usize, with: Self::Item) -> ReplaceNth<Self> where
    Self: Sized

An Iterator that replaces the nth element with another value.

Example

use core_extensions::iterators::ReplaceNth;

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

let list=vec!["hello","dear","world"];
assert_eq!(
    ReplaceNth::new( list.into_iter(),1,"my" ).collect::<Vec<_>>(),
    vec!["hello","my","world"]
);

Loading content...

Implementors

impl<I> IteratorExt for I where
    I: Iterator
[src]

Loading content...