Returning iterator of a Vec in a RefCell

You cannot do this because it would allow you to circumvent runtime checks for uniqueness violations. RefCell provides you a way to “defer” mutability exclusiveness checks to runtime, in exchange allowing mutation of the data it holds inside through shared references. This is done using RAII guards: you can obtain a guard object using a … Read more

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