Box<[T; N]> vs Box<[T]>

Just curious about it when I'm browsing around twir and find this PR: Add `Box<[T; N]>: TryFrom<Vec<T>>` by scottmcm · Pull Request #101837 · rust-lang/rust · GitHub

Box<[T]> stores continuous data on the heap with length fixed. So what's the advantage of using Box<[T; N]>. The latter is more limited in usage.

Compile time bounds checking / no or less run time bounds checking.

2 Likes

Box<[T]> takes up two usizes (ptr, len), Box<[T;N]> just needs one (ptr). Since the N is known at compile time it can help the compiler optimize. So the more limited one is potentially faster, and more memory efficient.

5 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.