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?
&[u8]
&[u8; 3]
As of Rust 1.34, you can use TryFrom / TryInto:
TryFrom
TryInto
use std::convert::TryFrom; fn example(slice: &[u8]) { let array = <&[u8; 3]>::try_from(slice); println!("{:?}", array); } fn example_mut(slice: &mut [u8]) { let array = <&mut [u8; 3]>::try_from(slice); println!("{:?}", array); }