Struct Peekable
struct Peekable<I: Iterator> { ... }
An iterator with a peek() that returns an optional reference to the next
element.
This struct is created by the peekable method on Iterator. See its
documentation for more.
Implementations
impl<I: Iterator> Peekable<I>
fn peek(self: &mut Self) -> Option<&<I as >::Item>Returns a reference to the next() value without advancing the iterator.
Like
next, if there is a value, it is wrapped in aSome(T). But if the iteration is over,Noneis returned.Because
peek()returns a reference, and many iterators iterate over references, there can be a possibly confusing situation where the return value is a double reference. You can see this effect in the examples below.Examples
Basic usage:
let xs = ; let mut iter = xs.iter.peekable; // peek() lets us see into the future assert_eq!; assert_eq!; assert_eq!; // The iterator does not advance even if we `peek` multiple times assert_eq!; assert_eq!; assert_eq!; // After the iterator is finished, so is `peek()` assert_eq!; assert_eq!;fn peek_mut(self: &mut Self) -> Option<&mut <I as >::Item>Returns a mutable reference to the next() value without advancing the iterator.
Like
next, if there is a value, it is wrapped in aSome(T). But if the iteration is over,Noneis returned.Because
peek_mut()returns a reference, and many iterators iterate over references, there can be a possibly confusing situation where the return value is a double reference. You can see this effect in the examples below.Examples
Basic usage:
let mut iter = .iter.peekable; // Like with `peek()`, we can see into the future without advancing the iterator. assert_eq!; assert_eq!; assert_eq!; // Peek into the iterator and set the value behind the mutable reference. if let Some = iter.peek_mut // The value we put in reappears as the iterator continues. assert_eq!;fn next_if<impl FnOnce(&I::Item) -> bool: FnOnce(&<I as >::Item) -> bool>(self: &mut Self, func: impl FnOnce(&<I as >::Item) -> bool) -> Option<<I as >::Item>Consume and return the next value of this iterator if a condition is true.
If
funcreturnstruefor the next value of this iterator, consume and return it. Otherwise, returnNone.Examples
Consume a number if it's equal to 0.
let mut iter = .peekable; // The first item of the iterator is 0; consume it. assert_eq!; // The next item returned is now 1, so `next_if` will return `None`. assert_eq!; // `next_if` retains the next item if the predicate evaluates to `false` for it. assert_eq!;Consume any number less than 10.
let mut iter = .peekable; // Consume all numbers less than 10 while iter.next_if.is_some // The next value returned will be 10 assert_eq!;fn next_if_eq<T>(self: &mut Self, expected: &T) -> Option<<I as >::Item> where T: ?Sized, <I as >::Item: PartialEq<T>Consume and return the next item if it is equal to
expected.Example
Consume a number if it's equal to 0.
let mut iter = .peekable; // The first item of the iterator is 0; consume it. assert_eq!; // The next item returned is now 1, so `next_if_eq` will return `None`. assert_eq!; // `next_if_eq` retains the next item if it was not equal to `expected`. assert_eq!;fn next_if_map<R, impl FnOnce(I::Item) -> Result<R, I::Item>: FnOnce(<I as >::Item) -> Result<R, <I as >::Item>>(self: &mut Self, f: impl FnOnce(<I as >::Item) -> Result<R, <I as >::Item>) -> Option<R>Consumes the next value of this iterator and applies a function
fon it, returning the result if the closure returnsOk.Otherwise if the closure returns
Errthe value is put back for the next iteration.The content of the
Errvariant is typically the original value of the closure, but this is not required. If a different value is returned, the nextpeek()ornext()call will result in this new value. This is similar to modifying the output ofpeek_mut().If the closure panics, the next value will always be consumed and dropped even if the panic is caught, because the closure never returned an
Errvalue to put back.See also:
next_if_map_mut.Examples
Parse the leading decimal number from an iterator of characters.
let mut iter = "125 GOTO 10".chars.peekable; let mut line_num = 0_u32; while let Some = iter.next_if_map assert_eq!; assert_eq!;Matching custom types.
/// Combines all consecutive `Comment` nodes into a single one. # assert_eq!fn next_if_map_mut<R, impl FnOnce(&mut I::Item) -> Option<R>: FnOnce(&mut <I as >::Item) -> Option<R>>(self: &mut Self, f: impl FnOnce(&mut <I as >::Item) -> Option<R>) -> Option<R>Gives a mutable reference to the next value of the iterator and applies a function
fto it, returning the result and advancing the iterator iffreturnsSome.Otherwise, if
freturnsNone, the next value is kept for the next iteration.If
fpanics, the item that is consumed from the iterator as ifSomewas returned fromf. The value will be dropped.This is similar to
next_if_map, except ownership of the item is not given tof. This can be preferable iffwould copy the item anyway.Examples
Parse the leading decimal number from an iterator of characters.
let mut iter = "125 GOTO 10".chars.peekable; let mut line_num = 0_u32; while let Some = iter.next_if_map_mut assert_eq!; assert_eq!;
impl<I> DoubleEndedIterator for Peekable<I>
fn next_back(self: &mut Self) -> Option<<Self as >::Item>fn try_rfold<B, F, R>(self: &mut Self, init: B, f: F) -> R where Self: Sized, F: FnMut(B, <Self as >::Item) -> R, R: Try<Output = B>fn rfold<Acc, Fold>(self: Self, init: Acc, fold: Fold) -> Acc where Fold: FnMut(Acc, <Self as >::Item) -> Acc
impl<I> Freeze for Peekable<I>
impl<I> IntoIterator for Peekable<I>
fn into_iter(self: Self) -> I
impl<I> RefUnwindSafe for Peekable<I>
impl<I> Send for Peekable<I>
impl<I> Sync for Peekable<I>
impl<I> TrustedLen for Peekable<I>
impl<I> Unpin for Peekable<I>
impl<I> UnsafeUnpin for Peekable<I>
impl<I> UnwindSafe for Peekable<I>
impl<I: $crate::clone::Clone + Iterator> Clone for Peekable<I>
fn clone(self: &Self) -> Peekable<I>
impl<I: $crate::fmt::Debug + Iterator> Debug for Peekable<I>
fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result
impl<I: ExactSizeIterator> ExactSizeIterator for Peekable<I>
impl<I: FusedIterator> FusedIterator for Peekable<I>
impl<I: Iterator> Iterator for Peekable<I>
fn next(self: &mut Self) -> Option<<I as >::Item>fn count(self: Self) -> usizefn nth(self: &mut Self, n: usize) -> Option<<I as >::Item>fn last(self: Self) -> Option<<I as >::Item>fn size_hint(self: &Self) -> (usize, Option<usize>)fn try_fold<B, F, R>(self: &mut Self, init: B, f: F) -> R where Self: Sized, F: FnMut(B, <Self as >::Item) -> R, R: Try<Output = B>fn fold<Acc, Fold>(self: Self, init: Acc, fold: Fold) -> Acc where Fold: FnMut(Acc, <Self as >::Item) -> Acc
impl<T> Any for Peekable<I>
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for Peekable<I>
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for Peekable<I>
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> CloneToUninit for Peekable<I>
unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)
impl<T> From for Peekable<I>
fn from(t: T) -> TReturns the argument unchanged.
impl<T, U> Into for Peekable<I>
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for Peekable<I>
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for Peekable<I>
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>