Let's say I want to double each value in an iterator of numbers. I could do this:
vec![1, 2, 3]
.into_iter()
.map(|x| x * 2)
.for_each(|x| println!("{}", x)); //Prints 2, 4, 6.
To get cleaner code, I would prefer to do this:
vec![1, 2, 3]
.into_iter()
.double() //I need to implement this.
.for_each(|x| println!("{}", x));
How do I write my own chainable iterator function, like double
in this example? I guess I will have to create an interface and implement it for Iterator? There are a lot of types to get right, so a working solution for this silly example would be helpful.