@@ -1141,6 +1141,35 @@ impl<T> MaybeUninit<T> {
1141
1141
///
1142
1142
/// Note that dropping a `MaybeUninit` will never call `T`'s drop code.
1143
1143
/// It is your responsibility to make sure `T` gets dropped if it got initialized.
1144
+ ///
1145
+ /// # Example
1146
+ ///
1147
+ /// Correct usage of this method: initializing a struct with zero, where all
1148
+ /// fields of the struct can hold 0 as a valid value.
1149
+ ///
1150
+ /// ```rust
1151
+ /// #![feature(maybe_uninit)]
1152
+ /// use std::mem::MaybeUninit;
1153
+ ///
1154
+ /// let x = MaybeUninit::<(u8, bool)>::zeroed();
1155
+ /// let x = unsafe { x.into_initialized() };
1156
+ /// assert_eq!(x, (0, false));
1157
+ /// ```
1158
+ ///
1159
+ /// *Incorrect* usage of this method: initializing a struct with zero, where some fields
1160
+ /// cannot hold 0 as a valid value.
1161
+ ///
1162
+ /// ```rust,no_run
1163
+ /// #![feature(maybe_uninit)]
1164
+ /// use std::mem::MaybeUninit;
1165
+ ///
1166
+ /// enum NotZero { One = 1, Two = 2 };
1167
+ ///
1168
+ /// let x = MaybeUninit::<(u8, NotZero)>::zeroed();
1169
+ /// let x = unsafe { x.into_initialized() };
1170
+ /// // We create a `NotZero` (inside a pair) that does not have a valid discriminant.
1171
+ /// // This is undefined behavior.
1172
+ /// ```
1144
1173
#[ unstable( feature = "maybe_uninit" , issue = "53491" ) ]
1145
1174
#[ inline]
1146
1175
pub fn zeroed ( ) -> MaybeUninit < T > {
0 commit comments