Create an iterator from a single element
You can use the Option
by-value iterator, into_iter
:
Some(2).into_iter().chain((3..).step_by(2))
This is the exact use case of std::iter::once
.
Creates an iterator that yields an element exactly once.
This is commonly used to adapt a single value into a
chain
of other kinds of iteration. Maybe you have an iterator that covers almost everything, but you need an extra special case. Maybe you have a function which works on iterators, but you only need to process one value.
range_step_inclusive
is long gone, so let's also use the inclusive range syntax (..=
):
iter::once(2).chain((3..=max).step_by(2))
It is not less boilerplate, but I guess it is more clear:
Repeat::new(2i).take(1).chain(range_step_inclusive(3, max, 2))
Repeat::new
will create an endless iterator from the value you provide. Take will yield just the first value of that iterator. The rest is nothing new.
You can run this example on the playpen following the link: http://is.gd/CZbxD3