expression
Convert a slice to a fixed array
For Copy elements, a slice can be copied into an owned array when the lengths match. A wrong length produces an error, not an automatically padded array. Expected stdout (not executed): [3, 7] true Common mistake: Assuming a shorter input will be padded to the requested length.
Formal Syntax & Structure
rust syntaxrust
let fixed: [T; N] = slice.try_into()?;Working Example
Example Snippetrust
fn main() { let data: &[u8] = &[3, 7]; let pair: [u8; 2] = data.try_into().unwrap(); let wrong: Result<[u8; 3], _> = data.try_into(); println!("{pair:?} {}", wrong.is_err());}