Using miri; it says its undefined behaviour but running it displays normally (unless it doesn’t?)
I don’t what IntoIter with arrays internally but I know maybe IntoIter stores the array internally
It is due to removing provenance?
Using miri; it says its undefined behaviour but running it displays normally (unless it doesn’t?)
I don’t what IntoIter with arrays internally but I know maybe IntoIter stores the array internally
It is due to removing provenance?
No it has nothing to do with provenance. When you do offset on a *mut [usize; 5] it doesn't offset by one usize, but by 5. So by doing the offset you move the pointer out of the array bounds and read completey outside of the array. That of course is UB.
If you run in release mode you get random values.
Also: even if you do the ptr arithmetic correctly you are still relying on the memory layout of the array iterator, which isn't specified, so while it isn't UB on the current compiler it could break any time.
I think the reason .offset(1) works in Debug mode is just coincidence, it is reading the old moved-from array on the stack which is no longer valid. So yes, this is UB.
Miri can be appeased here by removing the as usize and the .offset(1). If this code comes from a real world use case I'd recommend using the std::array::IntoIter's .as_slice() method.