File tree 1 file changed +7
-7
lines changed
1 file changed +7
-7
lines changed Original file line number Diff line number Diff line change @@ -173,32 +173,32 @@ use crate::ptr;
173
173
/// ## Initializing a struct field-by-field
174
174
///
175
175
/// You can use `MaybeUninit<T>`, and the [`std::ptr::addr_of_mut`] macro, to initialize structs field by field:
176
- ///
176
+ ///
177
177
/// ```rust
178
178
/// use std::mem::MaybeUninit;
179
179
/// use std::ptr::addr_of_mut;
180
- ///
180
+ ///
181
181
/// #[derive(Debug, PartialEq)]
182
182
/// pub struct Foo {
183
183
/// name: String,
184
184
/// list: Vec<u8>,
185
185
/// }
186
- ///
186
+ ///
187
187
/// let foo = {
188
188
/// let mut uninit: MaybeUninit<Foo> = MaybeUninit::uninit();
189
189
/// let ptr = uninit.as_mut_ptr();
190
- ///
190
+ ///
191
191
/// // Initializing the `name` field
192
192
/// unsafe { addr_of_mut!((*ptr).name).write("Bob".to_string()); }
193
- ///
193
+ ///
194
194
/// // Initializing the `list` field
195
195
/// // If there was a panic here, then the `String` in the `name` field would be leaked.
196
196
/// unsafe { addr_of_mut!((*ptr).list).write(vec![0, 1, 2]); }
197
- ///
197
+ ///
198
198
/// // All the fields are initialized, so we call `assume_init` to get an initialized Foo.
199
199
/// unsafe { uninit.assume_init() }
200
200
/// };
201
- ///
201
+ ///
202
202
/// assert_eq!(
203
203
/// foo,
204
204
/// Foo {
You can’t perform that action at this time.
0 commit comments