std-method
Initialize a zero-valid value with MaybeUninit::zeroed
Zero bytes are suitable only for types whose validity rules accept that representation. For an array of u8 this is fine. Zeroing storage is not a general constructor for references or nonzero integers. Do not call assume_init for those invalid representations. Expected stdout (not executed): [0, 0, 0] Common mistake: Assuming all-zero bytes represent a valid value of every type.
Formal Syntax & Structure
rust syntaxrust
MaybeUninit::<T>::zeroed()Working Example
Example Snippetrust
#![deny(unsafe_op_in_unsafe_fn)]fn main() { use std::mem::MaybeUninit; let slot = MaybeUninit::<[u8; 3]>::zeroed(); // SAFETY: every zero byte is a valid u8; all elements are initialized. let bytes = unsafe { slot.assume_init() }; println!("{bytes:?}");}