Replacing Box::new_uninit(), assume_init()
⚓ Rust 📅 2026-01-31 👤 surdeus 👁️ 8I am working on an ffi crate and realized I can target a fairly old rust toolchain with little effort.
However, I need to replace this pattern:
let mut mem: Box<MaybeUninit<sys::c_struct>> = Box::new_uninit();
// initialize in C
let mem: Box<sys::c_struct> = unsafe { mem.assume_init() };
let inner: *mut sys::c_struct= Box::into_raw(mem);
Then of course, dropped with Box::from_raw
I think it could be replaced with just the following:
let mut mem: Box<MaybeUninit<sys::c_struct >> = Box::new(MaybeUninit::uninit());
let inner: *mut sys::c_struct = Box::into_raw(mem) as *mut sys::c_struct;
Is this alright to do, or are there better ways?
Thanks
1 post - 1 participant
🏷️ Rust_feed