When would you want to use a boxed array?

I think it comes from technical limitations which make Box<[T; N]> unusable:

  • For most of its existence, Rust had very poor support for arrays longer than 32 elements. Longer arrays are very cumbersome to use without the const generics feature.

  • Box::new(arr) usually ends up with its arguments on the stack, which causes stack overflows for large arrays.

Since large arrays are unusable, and it's usually inefficient to box small arrays (in modern CPUs small copies are cheap, indirection and allocations can be expensive), there's little use for boxed arrays.

8 Likes