@@ -112,7 +112,6 @@ use crate::mem::ManuallyDrop;
112
112
///
113
113
/// ```
114
114
/// use std::mem::{self, MaybeUninit};
115
- /// use std::ptr;
116
115
///
117
116
/// let data = {
118
117
/// // Create an uninitialized array of `MaybeUninit`. The `assume_init` is
@@ -122,10 +121,13 @@ use crate::mem::ManuallyDrop;
122
121
/// MaybeUninit::uninit().assume_init()
123
122
/// };
124
123
///
125
- /// // Dropping a `MaybeUninit` does nothing, so if there is a panic during this loop,
126
- /// // we have a memory leak, but there is no memory safety issue.
124
+ /// // Dropping a `MaybeUninit` does nothing. Thus using raw pointer
125
+ /// // assignment instead of `ptr::write` does not cause the old
126
+ /// // uninitialized value to be dropped. Also if there is a panic during
127
+ /// // this loop, we have a memory leak, but there is no memory safety
128
+ /// // issue.
127
129
/// for elem in &mut data[..] {
128
- /// unsafe { ptr::write(elem.as_mut_ptr(), vec![42]); }
130
+ /// *elem = MaybeUninit::new( vec![42]);
129
131
/// }
130
132
///
131
133
/// // Everything is initialized. Transmute the array to the
@@ -151,7 +153,7 @@ use crate::mem::ManuallyDrop;
151
153
/// let mut data_len: usize = 0;
152
154
///
153
155
/// for elem in &mut data[0..500] {
154
- /// unsafe { ptr::write(elem.as_mut_ptr(), String::from("hello")); }
156
+ /// *elem = MaybeUninit::new( String::from("hello"));
155
157
/// data_len += 1;
156
158
/// }
157
159
///
0 commit comments