Skip to content

Commit a2c3406

Browse files
committed
Use inline const instead of unsafe to construct arrays in MaybeUninit examples.
1 parent 25245bb commit a2c3406

File tree

1 file changed

+4
-10
lines changed

1 file changed

+4
-10
lines changed

core/src/mem/maybe_uninit.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,8 @@ use crate::slice;
120120
/// use std::mem::{self, MaybeUninit};
121121
///
122122
/// let data = {
123-
/// // Create an uninitialized array of `MaybeUninit`. The `assume_init` is
124-
/// // safe because the type we are claiming to have initialized here is a
125-
/// // bunch of `MaybeUninit`s, which do not require initialization.
126-
/// let mut data: [MaybeUninit<Vec<u32>>; 1000] = unsafe {
127-
/// MaybeUninit::uninit().assume_init()
128-
/// };
123+
/// // Create an uninitialized array of `MaybeUninit`.
124+
/// let mut data: [MaybeUninit<Vec<u32>>; 1000] = [const { MaybeUninit::uninit() }; 1000];
129125
///
130126
/// // Dropping a `MaybeUninit` does nothing, so if there is a panic during this loop,
131127
/// // we have a memory leak, but there is no memory safety issue.
@@ -147,10 +143,8 @@ use crate::slice;
147143
/// ```
148144
/// use std::mem::MaybeUninit;
149145
///
150-
/// // Create an uninitialized array of `MaybeUninit`. The `assume_init` is
151-
/// // safe because the type we are claiming to have initialized here is a
152-
/// // bunch of `MaybeUninit`s, which do not require initialization.
153-
/// let mut data: [MaybeUninit<String>; 1000] = unsafe { MaybeUninit::uninit().assume_init() };
146+
/// // Create an uninitialized array of `MaybeUninit`.
147+
/// let mut data: [MaybeUninit<String>; 1000] = [const { MaybeUninit::uninit() }; 1000];
154148
/// // Count the number of elements we have assigned.
155149
/// let mut data_len: usize = 0;
156150
///

0 commit comments

Comments
 (0)