Are there equivalents to slice::chunks/windows for iterators to loop over pairs, triplets etc?

It’s possible to take chunks of an iterator using Itertools::tuples, up to a 4-tuple: use itertools::Itertools; // 0.9.0 fn main() { let some_iter = vec![1, 2, 3, 4, 5, 6].into_iter(); for (prev, next) in some_iter.tuples() { println!(“{}–{}”, prev, next); } } (playground) 1–2 3–4 5–6 If you don’t know that your iterator exactly fits into … Read more

How can I add new methods to Iterator?

In your particular case, it’s because you have implemented your trait for an iterator of String, but your vector is providing an iterator of &str. Here’s a more generic version: use std::collections::HashSet; use std::hash::Hash; struct Unique<I> where I: Iterator, { seen: HashSet<I::Item>, underlying: I, } impl<I> Iterator for Unique<I> where I: Iterator, I::Item: Hash + … Read more