So I have an array I've just created of u8 (a
) and a slice of u16 (b
). How can I get an array that is the result of concatenating the elements of a
with the elements of b
after turning these into u8?
I've tried a lot of modifications of the following code, but there is always some error.
fn main() {
let a: [u8; 3] = [0x01, 0x02, 0x03];
let b: &[u16] = &[0x0405, 0x0607, 0x0809];
let result = [a,
b.iter().flat_map(|s| &s.to_be_bytes()).collect()]
.iter().flat_map(|e| e.iter()).collect();
assert_eq!(result, [1, 2, 3, 4, 5, 6, 7, 8, 9]);
}
I understand the error in that piece of code but I don't know how to fix it without creating another.
Like, I can create a
as a Vec<u8>
so that collect()
can collect into something that implements std::iter::FromIterator
. But it collects into Vec<&u8>
.
I also have the feeling that it should be way more simple than my example. As if I'm missing a specific function.