4 ms·
It should be `v.into_iter()`, and that distinction matters because of ownership / move semantics. It just so happens that for most collections, `IntoIter` is a
by kevinmgranger 3mo ago
It should be `v.into_iter()`, and that distinction matters because of ownership / move semantics.
It just so happens that for most collections, `IntoIter` is also defined for references to them, which typically gives you the same behavior that `.iter()` would give.
- tialaramex 3mo agoMore specifically for x in y consumes y in Rust. https://doc.rust-lang.org/std/keyword.for.html https://doc.rust-lang.org/std/keyword.for.html explains how a loop is de-sugared https://rust.godbolt.org/z/5jzhxYM51 https://rust.godbolt.org/z/5jzhxYM51 ... shows that today ranges like 0..5 aren't Copy even if that would be possible, which means if they're consumed they're gone, whereas an array of integers is Copy and so consuming it doesn't mean it's gone, you can just consume it again. The desire is that Rust 2027 edition will change the nice syntax for ranges to produce new ranges like core::ranges::Range which are Copy if possible and only IntoIterator, the original ranges are never Copy but are Iterator, we now regret this choice.