This repository was archived by the owner on May 28, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +35
-4
lines changed Expand file tree Collapse file tree 1 file changed +35
-4
lines changed Original file line number Diff line number Diff line change @@ -172,10 +172,41 @@ use crate::ptr;
172
172
///
173
173
/// ## Initializing a struct field-by-field
174
174
///
175
- /// There is currently no supported way to create a raw pointer or reference
176
- /// to a field of a struct inside `MaybeUninit<Struct>`. That means it is not possible
177
- /// to create a struct by calling `MaybeUninit::uninit::<Struct>()` and then writing
178
- /// to its fields.
175
+ /// You can use `MaybeUninit<T>`, and the [`std::ptr::addr_of_mut`] macro, to initialize structs field by field:
176
+ ///
177
+ /// ```rust
178
+ /// use std::mem::MaybeUninit;
179
+ /// use std::ptr::addr_of_mut;
180
+ ///
181
+ /// #[derive(Debug, PartialEq)]
182
+ /// pub struct Foo {
183
+ /// name: String,
184
+ /// list: Vec<u8>,
185
+ /// }
186
+ ///
187
+ /// let foo = {
188
+ /// let mut uninit: MaybeUninit<Foo> = MaybeUninit::uninit();
189
+ /// let ptr = uninit.as_mut_ptr();
190
+ ///
191
+ /// // Initializing the `name` field
192
+ /// unsafe { addr_of_mut!((*ptr).name).write("Bob".to_string()); }
193
+ ///
194
+ /// // Initializing the `list` field
195
+ /// // If there was a panic here, then the `String` in the `name` field would be leaked.
196
+ /// unsafe { addr_of_mut!((*ptr).list).write(vec![0, 1, 2]); }
197
+ ///
198
+ /// // All the fields are initialized, so we call `assume_init` to get an initialized Foo.
199
+ /// unsafe { uninit.assume_init() }
200
+ /// };
201
+ ///
202
+ /// assert_eq!(
203
+ /// foo,
204
+ /// Foo {
205
+ /// name: "Bob".to_string(),
206
+ /// list: vec![0, 1, 2]
207
+ /// }
208
+ /// );
209
+ /// ```
179
210
///
180
211
/// [ub]: ../../reference/behavior-considered-undefined.html
181
212
///
You can’t perform that action at this time.
0 commit comments