// *** before: ***
// последовательность из замыкания требовала своего типа с impl Iterator
struct Countdown(u32);
impl Iterator for Countdown {
type Item = u32;
fn next(&mut self) -> Option<u32> {
if self.0 == 0 { return None; }
self.0 -= 1;
Some(self.0)
}
}
// *** in version 1.34: ***
let mut n = 3;
let countdown = std::iter::from_fn(move || {
if n == 0 { return None; }
n -= 1;
Some(n)
});
println!("{:?}", countdown.collect::<Vec<_>>()); // [2, 1, 0]