W3cubDocs

/Rust

Function std::mem::zeroed

pub unsafe fn zeroed<T>() -> T

Creates a value whose bytes are all zero.

This has the same effect as allocating space with mem::uninitialized and then zeroing it out. It is useful for FFI sometimes, but should generally be avoided.

There is no guarantee that an all-zero byte-pattern represents a valid value of some type T. If T has a destructor and the value is destroyed (due to a panic or the end of a scope) before being initialized, then the destructor will run on zeroed data, likely leading to undefined behavior.

See also the documentation for mem::uninitialized, which has many of the same caveats.

Examples

use std::mem;

let x: i32 = unsafe { mem::zeroed() };
assert_eq!(0, x);

© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/mem/fn.zeroed.html