Struct nom::lib::std::iter::Peekable 1.0.0[−][src]
#[must_use = "iterators are lazy and do nothing unless consumed"]pub struct Peekable<I> where
I: Iterator, { /* fields omitted */ }
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> Peekable<I> where
I: Iterator,
[src][−]
I: Iterator,
pub fn peek(&mut self) -> Option<&<I as Iterator>::Item>
[src][−]
Returns a reference to the next() value without advancing the iterator.
Like next
, if there is a value, it is wrapped in a Some(T)
.
But if the iteration is over, None
is 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 = [1, 2, 3]; let mut iter = xs.iter().peekable(); // peek() lets us see into the future assert_eq!(iter.peek(), Some(&&1)); assert_eq!(iter.next(), Some(&1)); assert_eq!(iter.next(), Some(&2)); // The iterator does not advance even if we `peek` multiple times assert_eq!(iter.peek(), Some(&&3)); assert_eq!(iter.peek(), Some(&&3)); assert_eq!(iter.next(), Some(&3)); // After the iterator is finished, so is `peek()` assert_eq!(iter.peek(), None); assert_eq!(iter.next(), None);
pub fn peek_mut(&mut self) -> Option<&mut <I as Iterator>::Item>
1.53.0[src][−]
Returns a mutable reference to the next() value without advancing the iterator.
Like next
, if there is a value, it is wrapped in a Some(T)
.
But if the iteration is over, None
is 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 = [1, 2, 3].iter().peekable(); // Like with `peek()`, we can see into the future without advancing the iterator. assert_eq!(iter.peek_mut(), Some(&mut &1)); assert_eq!(iter.peek_mut(), Some(&mut &1)); assert_eq!(iter.next(), Some(&1)); // Peek into the iterator and set the value behind the mutable reference. if let Some(p) = iter.peek_mut() { assert_eq!(*p, &2); *p = &5; } // The value we put in reappears as the iterator continues. assert_eq!(iter.collect::<Vec<_>>(), vec![&5, &3]);
pub fn next_if(
&mut self,
func: impl FnOnce(&<I as Iterator>::Item) -> bool
) -> Option<<I as Iterator>::Item>
1.51.0[src][−]
&mut self,
func: impl FnOnce(&<I as Iterator>::Item) -> bool
) -> Option<<I as Iterator>::Item>
Consume and return the next value of this iterator if a condition is true.
If func
returns true
for the next value of this iterator, consume and return it.
Otherwise, return None
.
Examples
Consume a number if it’s equal to 0.
let mut iter = (0..5).peekable(); // The first item of the iterator is 0; consume it. assert_eq!(iter.next_if(|&x| x == 0), Some(0)); // The next item returned is now 1, so `consume` will return `false`. assert_eq!(iter.next_if(|&x| x == 0), None); // `next_if` saves the value of the next item if it was not equal to `expected`. assert_eq!(iter.next(), Some(1));
Consume any number less than 10.
let mut iter = (1..20).peekable(); // Consume all numbers less than 10 while iter.next_if(|&x| x < 10).is_some() {} // The next value returned will be 10 assert_eq!(iter.next(), Some(10));
pub fn next_if_eq<T>(&mut self, expected: &T) -> Option<<I as Iterator>::Item> where
T: ?Sized,
<I as Iterator>::Item: PartialEq<T>,
1.51.0[src][−]
T: ?Sized,
<I as Iterator>::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 = (0..5).peekable(); // The first item of the iterator is 0; consume it. assert_eq!(iter.next_if_eq(&0), Some(0)); // The next item returned is now 1, so `consume` will return `false`. assert_eq!(iter.next_if_eq(&0), None); // `next_if_eq` saves the value of the next item if it was not equal to `expected`. assert_eq!(iter.next(), Some(1));
Trait Implementations
impl<I> Clone for Peekable<I> where
I: Clone + Iterator,
<I as Iterator>::Item: Clone,
[src][+]
I: Clone + Iterator,
<I as Iterator>::Item: Clone,
impl<I> Debug for Peekable<I> where
I: Debug + Iterator,
<I as Iterator>::Item: Debug,
[src][+]
I: Debug + Iterator,
<I as Iterator>::Item: Debug,
impl<I> DoubleEndedIterator for Peekable<I> where
I: DoubleEndedIterator,
1.38.0[src][+]
I: DoubleEndedIterator,
impl<I> ExactSizeIterator for Peekable<I> where
I: ExactSizeIterator,
[src][+]
I: ExactSizeIterator,
impl<I> FusedIterator for Peekable<I> where
I: FusedIterator,
1.26.0[src]
I: FusedIterator,
impl<I> InPlaceIterable for Peekable<I> where
I: InPlaceIterable,
[src]
I: InPlaceIterable,
impl<I> Iterator for Peekable<I> where
I: Iterator,
[src][+]
I: Iterator,
impl<S, I> SourceIter for Peekable<I> where
I: Iterator + SourceIter<Source = S>,
S: Iterator,
[src][+]
I: Iterator + SourceIter<Source = S>,
S: Iterator,
impl<I> TrustedLen for Peekable<I> where
I: TrustedLen,
[src]
I: TrustedLen,
Auto Trait Implementations
impl<I> Send for Peekable<I> where
I: Send,
<I as Iterator>::Item: Send,
I: Send,
<I as Iterator>::Item: Send,
impl<I> Sync for Peekable<I> where
I: Sync,
<I as Iterator>::Item: Sync,
I: Sync,
<I as Iterator>::Item: Sync,
impl<I> Unpin for Peekable<I> where
I: Unpin,
<I as Iterator>::Item: Unpin,
I: Unpin,
<I as Iterator>::Item: Unpin,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src][+]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src][+]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src][+]
T: ?Sized,
impl<T> From<T> for T
[src][+]
impl<T, U> Into<U> for T where
U: From<T>,
[src][+]
U: From<T>,
impl<I> IntoIterator for I where
I: Iterator,
[src][+]
I: Iterator,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src][+]
U: Into<T>,
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src][+]
U: TryFrom<T>,