Function malachite_base::iterators::first_and_last

source ·
pub fn first_and_last<I: Iterator>(xs: &mut I) -> Option<(I::Item, I::Item)>
where I::Item: Clone,
Expand description

Returns the first and last elements of an iterator, or None if it is empty.

The iterator’s elements must be cloneable, since if the iterator consists of a single element x, the result will be (x, x).

This iterator will hang if given an infinite iterator.

§Examples

use malachite_base::iterators::first_and_last;

let empty: [u32; 0] = [];
assert_eq!(first_and_last(&mut empty.iter()), None);
assert_eq!(first_and_last(&mut [1].iter().cloned()), Some((1, 1)));
assert_eq!(first_and_last(&mut [1, 2, 3].iter().cloned()), Some((1, 3)));