Constructing a Peekable?

  1. I have just written a custom iterator.

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

  3. I found: mod.rs - source

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!

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