I want to iterate over the bytes of an integer:
use core::mem::size_of;
const SIZE: usize = size_of::<u64>();
fn main() {
let x: u64 = 512;
let mut buf: [u8; SIZE] = [0; SIZE];
for (i, b) in x.to_be_bytes().into_iter().enumerate() {
buf[i] = b;
}
}
The compiler tells me for the line buf[i] = b;
that he expected `u8`, found `&u8`
. But why?
When I take a look at the implementation of the IntoIterator
trait for the owned array type [T; N]
the into_iter()
method returns a std::array::IntoIter<T, N>
which implements the Iterator
trait where type Item = T
. Shouldn't T
evaluate to u8
here?
Why is the iterator returning &u8
references instead of owned u8
bytes?