[<<][rust][>>][..]
Sat Feb 28 20:36:39 EST 2015

Are there any iterators that own an array?

I wrote this

struct InArr2 { a: [f32; 2], i: usize }
fn in_arr2(a: [f32;2]) -> InArr2 { InArr2 { a:a, i:0 } }
impl Iterator for InArr2 {
    type Item = f32;
    fn next(&mut self) -> Option<f32> {
        if self.i == self.a.len() { return None; }
        let rv = self.a[self.i];
        self.i += 1;
        return Some(rv);
    }
}

Because this

(0..2).flat_map(|i| [0,1].into_iter().cloned());

says that [0,1] doesn't live long enough.

I think the reason for that is that into_iter() takes a reference, not
ownership.


I think I understand why.  Arrays are a bit special in that they are
statically allocated.  It seems hard to do things like "turn an array
into an iterator" because the storage of the array should actually be
inside the iterator in the first place.  That is why the above works,
but it seems this is hard to do generically.




[Reply][About]
[<<][rust][>>][..]