Uninitialized Memory and How Rust Handles It
Rust normally refuses to let you read a value that has not been set, because whatever bits are there are meaningless. Sometimes you genuinely have a block of memory that something else will fill — hardware, or C code — so Rust gives you a wrapper that says "this may not be a valid value yet" and makes you assert when it finally is. It keeps the dangerous window explicit and short.
Questions this Concept answers
- Why is reading uninitialized memory undefined behaviour rather than merely returning junk?
MaybeUninit<T> - Unsafe Rust & FFI
Rust will not let you read a value that has never been set, because the bits sitting there could be anything. MaybeUninit<T> is the wrapper for the cases where you really do have raw memory that some…