Alternating Iterators
This crate aims to provides a convenient way to alternate between the items of two iterators. It allows you to iterate over two iterators in an alternating fashion, combining their elements into a single sequence.
For the simplest usage of this crate, bring the AlternatingExt trait into scope
use AlternatingExt;
and use the alternate_with method
to create new alternating iterators.
# use AlternatingExt;
let a = ;
let b = ;
let mut alternating = a.iter.alternate_with;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
By default the alternate_with method creates an iterator that returns an element from a first,
followed by element from b, and so on until both are exhausted.
Stopping after Exhaustion
If, however, you want the iteration to stop once either of the iterators is exhausted,
you can use the alternate_with_no_remainder method,
also provided by the AlternatingExt trait. This method returns an iterator that
stops as soon as it needs to return more than one item consecutively from a single iterator.
use AlternatingExt;
let a = ;
let b = ;
let mut iter = a.iter.alternate_with_no_remainder;
assert_eq!; // `a` first
assert_eq!; // `b`
assert_eq!; // `a`
assert_eq!; // `b`
assert_eq!; // remaining items from `b` are not returned
The iteration stops after the fourth element because returning the fifth element from b
would break the alternating pattern.