rust@octodon.social ("Rust tips") wrote:
Converting a slice
&[..]to a fixed-length array in #RustlangBy copying:
let arr: [_; N] = slice.try_into().unwrap();By reference:
let arr: &[_; N] = slice.first_chunk::<N>().unwrap();Single element only:
let arr: &[_; 1] = std::array::from_ref(element);If the slice length is obvious to the optimizer (e.g.
chunks_exact()iterator), theunwrap()will be optimized out. Otherwise, handle possibility of the slice being too short.There's more:
https://doc.rust-lang.org/stable/std/primitive.slice.html#method.split_first_chunk