Reversing a non-DoubleEndedIterator

Is there an existing adaptor in Iterator or itertools to reverse an iterator that doesn't natively support it? I was hoping for a iter.buffer() function equivalent to Vec::from_iter(iter).into_iter(), such that the resulting iterator implements DoubleEndedIterator even if the incoming iterator doesn't, so that rev() can be called on the result.

I don't know of an all-in-one, but you can turbofish .collect::<Vec<_>>().into_iter(), or with Itertools that's .collect_vec().into_iter(). You can also combine that with their function rev that converts to iterator for you, like rev(iter.collect_vec()).

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.