Help with making a custom peekable iterator

Hi! :wave:

I'm trying to make a custom Peekable iterator, which (following the standard implementation [1]) involves customizing several of the provided Iterator methods to take the peeked item into account when iterating.

The problem I'm having is that Iterator::try_fold uses the unstable Try trait in its signature, so then I don't see how I can provide a customized implementation of it using stable Rust.

To get around this I'd be OK with disabling try_fold for my iterator, but I think that's not possible? Is there something else I can try here?


If you don't implement try_fold, you just get the default implementation, which uses your next method.

1 Like

Thanks that makes sense, so then is the Peekable::try_fold customization unnecessary? I'm reading its implementation now and it seems like it could be removed and the behaviour would be equivalent, but maybe I'm missing something?

Overriding try_fold is done for performance, but it should have the same result either way. The idea is that next has to check its peek state every time, but when folding we can just check it once and then power through the rest of the iterator.


I see, thanks!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.