Array Iterator
Allows creating owning iterators out of arrays. This is useful for fixed-size iterators. Try writing the following function:
fn pair_iter(a: i32, b: i32) -> impl Iterator<Item=i32> {
unimplemented!()
}
This is hard to do because most iterators don't own the values they are iterating over. One ugly solution is:
fn pair_iter(a: i32, b: i32) -> impl Iterator<Item=i32> {
Some(a).into_iter().chain(Some(b))
}
assert_eq!(pair_iter(1, 2).collect::<Vec<_>>(), &[1, 2]);
This crate allows turning arrays into owning iterators, perfect for short, fixed-size iterators.
extern crate array_iterator;
use array_iterator::ArrayIterator;
fn pair_iter(a: i32, b: i32) -> impl Iterator<Item=i32> {
ArrayIterator::new([a, b])
}
assert_eq!(pair_iter(1, 2).collect::<Vec<_>>(), &[1, 2]);