3 ms·
I think your mental model is wrong if you thought a consuming operation like IntoIterator::into_iter is read only. The reason the first one works is that [T; N
by tialaramex 3y ago
I think your mental model is wrong if you thought a consuming operation like IntoIterator::into_iter is read only.
The reason the first one works is that [T; N] is Copy if T is Copy, and i32 is Copy so therefore [i32; 3] is Copy. So it's actually consuming two arrays, each of those for loops consumes the array to make an iterator, but since it's Copy it just leaves a perfectly good copy of the array behind in the variable. The compiler can see what we're doing here and won't pointlessly duplicate the array (presumably) but that's conceptually what's happening.
The second one doesn't work because the Vec<i32> isn't Copy, so, we consume it and now it's gone, when we reach the second for loop the variable numbers2 is gone already.
- fauigerzigerk 3y agoAs I said, I understand what's going on here. I just don't like it.