IntoIterator should be implemented for [T; N] not just references · Issue #25725 · rust-lang/rust (original) (raw)
Currently IntoIterator is implemented for arrays of &'a mut [T; N]
and &'a [T; N]
but not for [T; N]
making it only possible to collect()
arrays of references, not arrays of values:
// These are the same in spite of iter()
changing to into_iter()
.
let vec_ref1: Vec<&i32> = [1, 2].iter().collect();
let vec_ref2: Vec<&i32> = [1, 2].into_iter().collect();
// This is what into_iter()
should do but it doesn't actually work.
let vec: Vec = [1, 2].into_iter().collect();
This is something users are likely to run headlong into (probably accidentally) when testing out examples from the iterator docs. So, IntoIterator should be implemented so that last option is possible. The API stabilization RFC: rust-lang/rfcs#1105 states:
any breakage in a minor release ... it must always be possible to locally fix the problem through some kind of disambiguation that could have been done in advance
Meaning this would be allowed as a minor change in spite of [1, 2].into_iter()
behavior changing because it could be changed beforehand to [1, 2].iter()
.