Raw pointer of array and pointer of slice in Rust

Advertisements I’m trying to better understand Rust’s raw pointers, and I thought the following code would fail (sorry for possible UB) [Playground]: fn main() { println!("size of * const u32 ->{}", std::mem::size_of::<* const u32>()); println!("size of * const [u32;13] ->{}", std::mem::size_of::<* const [u32;13]>()); println!("size of * const [u32] ->{}", std::mem::size_of::<* const [u32]>()); let x =… Read More Raw pointer of array and pointer of slice in Rust

How to return slice of a vector in a struct

Advertisements I want to return a slice of my vector, but the compiler is complaining that &[Letter] needs an explicit lifetime. struct Board { board: Vec<Letter>, width: usize, height: usize, } impl std::ops::Index<usize> for Board { type Output = &[Letter]; fn index(&self, index: usize) -> &Self::Output { return &&self.board[index * self.width..(index + 1) * self.width];… Read More How to return slice of a vector in a struct

How to split a slice into a header array reference and a tail slice?

Advertisements I’m looking for a function with a signature similar to: split_header_and_tail(buf: &[u8]) -> Option<(&[u8; HEADER_LENGTH], &[u8])> If the supplied slice is too short, the result is None, but if it’s long enough a reference to the first HEADER_LENGTH bytes and the tail slice are returned. The first part must be a reference to a… Read More How to split a slice into a header array reference and a tail slice?