Function new_uninit

Source
pub unsafe fn new_uninit<T, const N: usize>() -> Result<Box<[MaybeUninit<T>; N]>, LayoutError>
Expand description

Allocates [T; N] on the heap.

For now, this function uses the global allocator. This will change once the Allocator trait becomes stable.

§Safety

The result of this function is undefined if mem::size_of::<T>() == 0, or if N == 0

§Errors

See Layout::array

§Examples

use std::mem;

unsafe {
    const LEN: usize = 1024 * 1024 * 1024;

    let mut arr = heap_arr::new_uninit::<usize, LEN>().unwrap();
    for (i, v) in arr.as_mut().iter_mut().enumerate() {
        v.write(i);
    }

    let arr: Box::<[usize; LEN]> = mem::transmute(arr);
}