std-method
slice::split_first_chunk_mut
Obtain exclusive access to a fixed-length prefix and its remaining slice. Update a header independently of its payload. Expected stdout (not executed): [8, 9, 0, 0] Common mistake: Trying to use the original packet while both mutable references are still needed.
Formal Syntax & Structure
rust syntaxrust
slice.split_first_chunk_mut::<N>()Working Example
Example Snippetrust
fn main() { let mut packet = [1, 2, 3, 4]; let (header, body) = packet.split_first_chunk_mut::<2>().unwrap(); *header = [8, 9]; body.fill(0); println!("{packet:?}");}