Mastodon Feed: Post

Mastodon Feed

rust@octodon.social ("Rust tips") wrote:

You can convert a slice (or a `Vec`) to a fixed-length array using the `TryInto` trait:

let arr: \[\_; 4\] = slice\[..4\].try\_into().unwrap();

It's a bit verbose, but the `.try_into().unwrap()` part will be optimized out entirely whenever the compiler can see the slice is long enough.

https://rust.godbolt.org/z/GGTnr1Pqz #rustlang