{
"$type": "site.standard.document",
"bskyPostRef": {
"cid": "bafyreiac6dgjuwpqa74w7g5tduq3os7znznxlez3snnuffv46yklqv2jye",
"uri": "at://did:plc:ivbknywyskln22er3nkssdhl/app.bsky.feed.post/3mjl46ipj3ow2"
},
"path": "/t/introduce-box-new-uninit-array-and-box-new-zeroed-array/24179#post_3",
"publishedAt": "2026-04-15T23:36:32.000Z",
"site": "https://internals.rust-lang.org",
"tags": [
"doc.rust-lang.org",
"Box in std::boxed - Rust",
"github.com/rust-lang/libs-team",
"Add array-equivalent API for Box slice functions",
"nahla-nee"
],
"textContent": "There exists a TryFrom impl for converting between a boxed slice and a boxed array, no need for unsafe:\n\ndoc.rust-lang.org\n\n### Box in std::boxed - Rust\n\nA pointer type that uniquely owns a heap allocation of type `T`.\n\ngithub.com/rust-lang/libs-team\n\n#### Add array-equivalent API for Box slice functions\n\nopened 11:11PM - 07 Mar 26 UTC\n\nclosed 12:50AM - 08 Mar 26 UTC\n\n\n\n nahla-nee\n \n\nT-libs-api api-change-proposal\n\n# Proposal ## Problem statement The `Box` type offers functionality for workin…g with slices, but does not have an equivalent for working with arrays whose size is known at compile time. The current workarounds involve writing obtuse unsafe code which is not ideal. ## Motivating examples or use cases The following is an example of constructing a `[usize; M]` array using Box, where `M` is some constant known at compile time: ```rs let mut free_list: Box<[MaybeUninit<usize>]> = Box::new_uninit_slice(M); for (idx, entry) in free_list.iter_mut().enumerate() { entry.write(idx); } // safety: all values have been initialized let free_list: Box<[usize; M]> = unsafe { Box::from_raw( Box::into_raw( free_list.assume_init() // turn from [MaybeUninit<usize>] to [usize] ) // get the pointer .cast() // cast from [usize] to [usize; M] ) // reconstruct to Box<[usize; M]> }; ``` This example, as the name of the variable implies, is motivated by code I'm writing which implements a free list for some buffer. This would also be useful for implementing large fixed size buffers which would otherwise be too large to create with `Box::new`, e.g. a Linux kernel UMEM buffer. ## Solution sketch The proposed API is as follows: ```rs impl<T, const N: usize> Box<[T; N]> { fn new_uninit_array() -> Box<[MaybeUninit<T>; N] { ... } fn new_zeroed_array() -> Box<[MaybeUninit<T>; N] { ... } fn try_new_uninit_array() -> Result<Box<[MaybeUninit<T>; N], AllocError> { ... } fn try_new_zeroed_array() -> Result<Box<[MaybeUninit<T>; N], AllocError> { ... } } impl<T, A> Box<[MaybeUninit<T>; N], A> { fn assume_init(self) -> Box<[T; N], A> { ... } } ``` It's not clear to me whether or not *_in functions should be part of this proposal or a different one, but those should likely be added to keep in line with the existing `Box` API. The above suggestion would transform the motivating example code to the following: ```rs let mut free_list: Box<[MaybeUninit<usize>; M]> = Box::new_uninit_array(); for (idx, entry) in free_list.iter_mut().enumerate() { entry.write(idx); } let free_list: Box<[usize; M]> = unsafe { free_list.assume_init() }; ``` ## Alternatives As was shown above it is possible to work around this using existing APIs, but it makes the code unnecessarily confusing. This addition would minimize the size and complexity of unsafe blocks. This could be implemented as a crate, but the orphan rule would make the API awkward at best. This also feels like an inappropriate solution. ## Links and related work I couldn't find any relevant discussion regarding this topic specifically on the Rust internals forum or in any GitHub issues. P.S. This is my first time creating an issue/proposal for the Rust project, so I hope I did it right! Let me know if anything needs modification. I would be happy to implement this feature myself as well.",
"title": "Introduce `Box::new_uninit_array` and `Box::new_zeroed_array`"
}