What is the best way to fill a slice from an iterator in Rust?

Viewed 3175

I'm implementing FromIterator for [MyStruct;4] where MyStruct is a small Copy struct. My current implementation is

fn from_iter<I: IntoIterator<Item=MyStruct>>(iter: I) -> Self {
    let mut retval = [Default::default();4];

    for (ret, src) in retval.iter_mut().zip(iter) {
        *ret = src;
    }

    retval
}

This works just fine, however I'm not sure that the for loop is as idiomatic as it could be. Is there perhaps a method like Slice::fill(iter) that could accomplish this more cleanly (and perhaps more efficiently)?

1 Answers
Related