pub const unsafe fn zeroed<T>() -> TExpand description
Returns the value of type T represented by the all-zero byte-pattern.
This means that, for example, the padding byte in (u8, u16) is not
necessarily zeroed.
This has the same effect as MaybeUninit::zeroed().assume_init().
It is useful for FFI sometimes, but should generally be avoided.
§Safety
The all-zero byte-pattern must represent a valid value of type T.
For example, it is not valid for reference types (&T, &mut T) or function
pointers. Using zeroed on such types causes immediate undefined behavior
because the Rust compiler assumes that there always is a valid value in a
variable it considers initialized.
§Examples
Correct usage of this function: initializing an integer with zero.
use std::mem;
let x: i32 = unsafe { mem::zeroed() };
assert_eq!(0, x);Incorrect usage of this function: initializing a reference with zero.
use std::mem;
let _x: &i32 = unsafe { mem::zeroed() }; // Undefined behavior!
let _y: fn() = unsafe { mem::zeroed() }; // And again!