Struct prev_iter::PrevPeekable [] [src]

pub struct PrevPeekable<I> where
    I: Iterator,
    <I as Iterator>::Item: Clone
{ /* fields omitted */ }

An iterator with prev(), prev_peek(), and peek() functions that return the previous element, a reference to the previous element, or a reference to the next element, respectively.

This struct is created by passing an Iterator whose Item implements Clone to the new function.

Methods

impl<I> PrevPeekable<I> where
    I: Iterator,
    <I as Iterator>::Item: Clone
[src]

[src]

Creates a new PrevPeekable. It takes an Iterator whose Item implements Clone.

Examples

Basic usage:

use prev_iter::PrevPeekable;

let v = vec![1, 2, 3];
let mut iter = PrevPeekable::new(v.iter());

assert_eq!(Some(&1), iter.next());
assert_eq!(Some(&2), iter.next());
assert_eq!(Some(&1), iter.prev());

[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);

[src]

Returns the previous value in the iterator without moving the iterator backwards. When the end is reached, it will always return the last element.

This function performs a clone() when returning the data.

Examples

Basic usage:

use prev_iter::PrevPeekable;

let v = vec![1, 2];
let mut it = PrevPeekable::new(v.iter());

// When the iterator is initialized there is not previous value
assert_eq!(None, it.prev());
assert_eq!(Some(&1), it.next());

// There is no value before the first element
assert_eq!(None, it.prev());
assert_eq!(Some(&2), it.next());

// Previous value before 2 is 1
assert_eq!(Some(&1), it.prev());

// The iterator doesn't have anymore values so the prev() will always
// return the last element
assert_eq!(None, it.next());
assert_eq!(Some(&2), it.prev());

[src]

Returns a reference to the previous value in the iterator without moving the iterator backwards. When the end is reached, it will always return the last element.

Because prev_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:

use prev_iter::PrevPeekable;

let v = vec![1, 2];
let mut it = PrevPeekable::new(v.iter());

// Initially there is nothing to peek at
assert_eq!(None, it.prev_peek());
assert_eq!(Some(&1), it.next());
 
// There is nothing before the first element
assert_eq!(None, it.prev_peek());
assert_eq!(Some(&2), it.next());

// 1 comes before 2
assert_eq!(Some(&&1), it.prev_peek());
assert_eq!(None, it.next());

// 2 will always be returned as the last element
assert_eq!(Some(&&2), it.prev_peek());

Trait Implementations

impl<I> Iterator for PrevPeekable<I> where
    I: Iterator,
    <I as Iterator>::Item: Clone
[src]

The type of the elements being iterated over.

[src]

Advances the iterator and returns the next value. Read more

1.0.0
[src]

Returns the bounds on the remaining length of the iterator. Read more

1.0.0
[src]

Consumes the iterator, counting the number of iterations and returning it. Read more

1.0.0
[src]

Consumes the iterator, returning the last element. Read more

1.0.0
[src]

Returns the nth element of the iterator. Read more

[src]

🔬 This is a nightly-only experimental API. (iterator_step_by)

unstable replacement of Range::step_by

Creates an iterator starting at the same point, but stepping by the given amount at each iteration. Read more

1.0.0
[src]

Takes two iterators and creates a new iterator over both in sequence. Read more

1.0.0
[src]

'Zips up' two iterators into a single iterator of pairs. Read more

1.0.0
[src]

Takes a closure and creates an iterator which calls that closure on each element. Read more

1.21.0
[src]

Calls a closure on each element of an iterator. Read more

1.0.0
[src]

Creates an iterator which uses a closure to determine if an element should be yielded. Read more

1.0.0
[src]

Creates an iterator that both filters and maps. Read more

1.0.0
[src]

Creates an iterator which gives the current iteration count as well as the next value. Read more

1.0.0
[src]

Creates an iterator which can use peek to look at the next element of the iterator without consuming it. Read more

1.0.0
[src]

Creates an iterator that [skip]s elements based on a predicate. Read more

1.0.0
[src]

Creates an iterator that yields elements based on a predicate. Read more

1.0.0
[src]

Creates an iterator that skips the first n elements. Read more

1.0.0
[src]

Creates an iterator that yields its first n elements. Read more

1.0.0
[src]

An iterator adaptor similar to [fold] that holds internal state and produces a new iterator. Read more

1.0.0
[src]

Creates an iterator that works like map, but flattens nested structure. Read more

[src]

🔬 This is a nightly-only experimental API. (iterator_flatten)

Creates an iterator that flattens nested structure. Read more

1.0.0
[src]

Creates an iterator which ends after the first [None]. Read more

1.0.0
[src]

Do something with each element of an iterator, passing the value on. Read more

1.0.0
[src]

Borrows an iterator, rather than consuming it. Read more

1.0.0
[src]

Transforms an iterator into a collection. Read more

1.0.0
[src]

Consumes an iterator, creating two collections from it. Read more

[src]

🔬 This is a nightly-only experimental API. (iterator_try_fold)

An iterator method that applies a function as long as it returns successfully, producing a single, final value. Read more

[src]

🔬 This is a nightly-only experimental API. (iterator_try_fold)

An iterator method that applies a fallible function to each item in the iterator, stopping at the first error and returning that error. Read more

1.0.0
[src]

An iterator method that applies a function, producing a single, final value. Read more

1.0.0
[src]

Tests if every element of the iterator matches a predicate. Read more

1.0.0
[src]

Tests if any element of the iterator matches a predicate. Read more

1.0.0
[src]

Searches for an element of an iterator that satisfies a predicate. Read more

1.0.0
[src]

Searches for an element in an iterator, returning its index. Read more

1.0.0
[src]

Searches for an element in an iterator from the right, returning its index. Read more

1.0.0
[src]

Returns the maximum element of an iterator. Read more

1.0.0
[src]

Returns the minimum element of an iterator. Read more

1.6.0
[src]

Returns the element that gives the maximum value from the specified function. Read more

1.15.0
[src]

Returns the element that gives the maximum value with respect to the specified comparison function. Read more

1.6.0
[src]

Returns the element that gives the minimum value from the specified function. Read more

1.15.0
[src]

Returns the element that gives the minimum value with respect to the specified comparison function. Read more

1.0.0
[src]

Reverses an iterator's direction. Read more

1.0.0
[src]

Converts an iterator of pairs into a pair of containers. Read more

1.0.0
[src]

Creates an iterator which [clone]s all of its elements. Read more

1.0.0
[src]

Repeats an iterator endlessly. Read more

1.11.0
[src]

Sums the elements of an iterator. Read more

1.11.0
[src]

Iterates over the entire iterator, multiplying all the elements Read more

1.5.0
[src]

Lexicographically compares the elements of this Iterator with those of another. Read more

1.5.0
[src]

Lexicographically compares the elements of this Iterator with those of another. Read more

1.5.0
[src]

Determines if the elements of this Iterator are equal to those of another. Read more

1.5.0
[src]

Determines if the elements of this Iterator are unequal to those of another. Read more

1.5.0
[src]

Determines if the elements of this Iterator are lexicographically less than those of another. Read more

1.5.0
[src]

Determines if the elements of this Iterator are lexicographically less or equal to those of another. Read more

1.5.0
[src]

Determines if the elements of this Iterator are lexicographically greater than those of another. Read more

1.5.0
[src]

Determines if the elements of this Iterator are lexicographically greater than or equal to those of another. Read more

Auto Trait Implementations

impl<I> Send for PrevPeekable<I> where
    I: Send,
    <I as Iterator>::Item: Send

impl<I> Sync for PrevPeekable<I> where
    I: Sync,
    <I as Iterator>::Item: Sync