I have an &[u8]
and would like to turn it into an &[u8; 3]
without copying. It should reference the original array. How can I do this?
They arrayref crate implements this.
Here's an example, you can of course use it in different ways:
#[macro_use]
extern crate arrayref;
/// Get the first 3 elements of `bytes` as a reference to an array
/// **Panics** if `bytes` is too short.
fn first3(bytes: &[u8]) -> &[u8; 3] {
array_ref![bytes, 0, 3]
}