I am playing around with generators, and I’ve found this problem:
https://play.rustlang.org/?gist=d444ef18e929818ed53dfba1b3e4c847&version=nightly
#![feature(inclusive_range_syntax, generator_trait,
generators, conservative_impl_trait)]
use std::ops::{Generator, GeneratorState};
fn generator_to_iterator<G>(g: G) > impl Iterator<Item = G::Yield>
where G: Generator<Return = ()> {
struct It<G>(G);
impl<G: Generator<Return = ()>> Iterator for It<G> {
type Item = G::Yield;
fn next(&mut self) > Option<Self::Item> {
match self.0.resume() {
GeneratorState::Yielded(y) => Some(y),
GeneratorState::Complete(()) => None,
}
}
}
It(g)
}
fn digits_u32(mut n: u32) > impl Iterator<Item=u32> {
generator_to_iterator(move  {
if n == 0 {
yield 0;
} else {
const LEN: usize = 10;
let mut digits = [0u32; LEN];
let mut idx = LEN  1;
while n != 0 {
digits[idx] = n % 10;
n /= 10;
idx = 1;
}
for &d in &digits[idx + 1 ..] { yield d; }
//for i in idx + 1 .. LEN { yield digits[i]; }
}
})
}
fn main() {
for i in 0 .. 100 {
print!("{}: ", i);
for d in digits_u32(i) {
print!("{} ", d);
}
println!();
}
}
digits_u32() is supposed to yield the digits of a u32 in order (yielding them in reversed order is simpler), the error it gives is:
> ...\test.rs:25:5

25  generator_to_iterator(move  {
 ^^^^^^^^^^^^^^^^^^^^^ `[u32]` does not have a constant size known at compiletime

= help: the trait `std::marker::Sized` is not implemented for `[u32]`
= note: only the last element of a tuple may have a dynamically sized type
As you see from the commented out line I’ve already found a workaround, but is this supposed to happen, and do you know if there’s a better workaround? Thank you.