@@ -1178,6 +1178,31 @@ impl<T> MaybeUninit<T> {
1178
1178
/// It is up to the caller to guarantee that the `MaybeUninit` really is in an initialized
1179
1179
/// state. Calling this when the content is not yet fully initialized causes undefined
1180
1180
/// behavior.
1181
+ ///
1182
+ /// # Examples
1183
+ ///
1184
+ /// Correct usage of this method:
1185
+ ///
1186
+ /// ```rust
1187
+ /// #![feature(maybe_uninit)]
1188
+ /// use std::mem::MaybeUninit;
1189
+ ///
1190
+ /// let mut x = MaybeUninit::<bool>::uninitialized();
1191
+ /// x.set(true);
1192
+ /// let x_init = unsafe { x.into_initialized() };
1193
+ /// assert_eq!(x_init, true);
1194
+ /// ```
1195
+ ///
1196
+ /// *Incorrect* usage of this method:
1197
+ ///
1198
+ /// ```rust,no_run
1199
+ /// #![feature(maybe_uninit)]
1200
+ /// use std::mem::MaybeUninit;
1201
+ ///
1202
+ /// let x = MaybeUninit::<Vec<u32>>::uninitialized();
1203
+ /// let x_init = unsafe { x.into_initialized() };
1204
+ /// // `x` had not been initialized yet, so this last line causes undefined behavior.
1205
+ /// ```
1181
1206
#[ unstable( feature = "maybe_uninit" , issue = "53491" ) ]
1182
1207
#[ inline( always) ]
1183
1208
pub unsafe fn into_initialized ( self ) -> T {
@@ -1212,15 +1237,17 @@ impl<T> MaybeUninit<T> {
1212
1237
/// let x1 = unsafe { x.read_initialized() };
1213
1238
/// // `u32` is `Copy`, so we may read multiple times.
1214
1239
/// let x2 = unsafe { x.read_initialized() };
1240
+ /// assert_eq!(x1, x2);
1215
1241
///
1216
1242
/// let mut x = MaybeUninit::<Option<Vec<u32>>>::uninitialized();
1217
1243
/// x.set(None);
1218
1244
/// let x1 = unsafe { x.read_initialized() };
1219
1245
/// // Duplicating a `None` value is okay, so we may read multiple times.
1220
1246
/// let x2 = unsafe { x.read_initialized() };
1247
+ /// assert_eq!(x1, x2);
1221
1248
/// ```
1222
1249
///
1223
- /// *Incorrect* usafe of this method:
1250
+ /// *Incorrect* usage of this method:
1224
1251
///
1225
1252
/// ```rust,no_run
1226
1253
/// #![feature(maybe_uninit)]
0 commit comments