Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions library/core/src/mem/maybe_uninit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,12 @@ impl<T> MaybeUninit<T> {
///
/// [inv]: #initialization-invariant
///
/// In particular, it is **not** enough for the memory to be of a type for which
/// any bit pattern is valid (such as `u8`, `i32`, or other integer types):
/// uninitialized memory is not a "valid bit pattern" since uninitialized data
/// is not a fixed value and reading it multiple times can produce different
/// results. See the [type-level documentation][inv] for more details.
///
/// On top of that, remember that most types have additional invariants beyond merely
/// being considered initialized at the type level. For example, a `1`-initialized [`Vec<T>`]
/// is considered initialized (under the current implementation; this does not constitute
Expand Down Expand Up @@ -695,6 +701,18 @@ impl<T> MaybeUninit<T> {
/// let x_init = unsafe { x.assume_init() };
/// // `x` had not been initialized yet, so this last line caused undefined behavior. ⚠️
/// ```
///
/// Even for types where every bit pattern is valid, reading uninitialized
/// memory is still undefined behavior:
///
/// ```rust,no_run
/// use std::mem::MaybeUninit;
///
/// let x = MaybeUninit::<u8>::uninit();
/// let x_init = unsafe { x.assume_init() };
/// // `x` had not been initialized yet, so this last line caused undefined
/// // behavior, even though `u8` can hold any fixed bit pattern! ⚠️
/// ```
#[stable(feature = "maybe_uninit", since = "1.36.0")]
#[rustc_const_stable(feature = "const_maybe_uninit_assume_init_by_value", since = "1.59.0")]
#[inline(always)]
Expand Down
Loading