Constructing a Peekable?

  1. I have just written a custom iterator.

  2. I would like to support peek on this iterator.

  3. I found: https://doc.rust-lang.org/src/core/iter/adapters/mod.rs.html#1072

My understanding is that if we have

pub struct MyIter;
impl Iterator for MyIter;

then, if I do Peekable<MyIter> , it should return to me an Iterator that supports .peek()

Now, unfortunately, the constructor Peekable::new is not public outside the crate. Thus, how do I construct a Peekable ?

From https://doc.rust-lang.org/std/iter/struct.Peekable.html:

This struct is created by the peekable method on Iterator . See its documentation for more.

1 Like

.peekable() worked. Thanks!