How to use adapters/closures for IntoIterator implementation?

Eventually we’ll have type_alias_impl_trait for this kind of problem

#![feature(type_alias_impl_trait)]
struct Evens(Vec<u32>);

type EvensIntoIterClosure = impl FnMut(&u32) -> bool;

impl IntoIterator for Evens {
    type Item=u32;
    type IntoIter=std::iter::Filter<<Vec<u32> as IntoIterator>::IntoIter, EvensIntoIterClosure>;
    
    fn into_iter(self)->Self::IntoIter {
        self.0.into_iter().filter(|x| *x % 2 == 0)
    }
}

fn main() {
    for x in Evens(vec![1,2,3,4,5,6,7,8,9,10]) {
        println!("{:?}", x);
    } 
}

(playground)

1 Like