Skip to content

Commit

Permalink
Use inline const instead of unsafe to construct arrays in `MaybeUnini…
Browse files Browse the repository at this point in the history
…t` examples.
  • Loading branch information
kpreid committed Jun 4, 2024
1 parent 44701e0 commit ac96fa4
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions library/core/src/mem/maybe_uninit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,8 @@ use crate::slice;
/// use std::mem::{self, MaybeUninit};
///
/// let data = {
/// // Create an uninitialized array of `MaybeUninit`. The `assume_init` is
/// // safe because the type we are claiming to have initialized here is a
/// // bunch of `MaybeUninit`s, which do not require initialization.
/// let mut data: [MaybeUninit<Vec<u32>>; 1000] = unsafe {
/// MaybeUninit::uninit().assume_init()
/// };
/// // Create an uninitialized array of `MaybeUninit`.
/// let mut data: [MaybeUninit<Vec<u32>>; 1000] = [const { MaybeUninit::uninit() }; 1000];
///
/// // Dropping a `MaybeUninit` does nothing, so if there is a panic during this loop,
/// // we have a memory leak, but there is no memory safety issue.
Expand All @@ -147,10 +143,8 @@ use crate::slice;
/// ```
/// use std::mem::MaybeUninit;
///
/// // Create an uninitialized array of `MaybeUninit`. The `assume_init` is
/// // safe because the type we are claiming to have initialized here is a
/// // bunch of `MaybeUninit`s, which do not require initialization.
/// let mut data: [MaybeUninit<String>; 1000] = unsafe { MaybeUninit::uninit().assume_init() };
/// // Create an uninitialized array of `MaybeUninit`.
/// let mut data: [MaybeUninit<String>; 1000] = [const { MaybeUninit::uninit() }; 1000];
/// // Count the number of elements we have assigned.
/// let mut data_len: usize = 0;
///
Expand Down

0 comments on commit ac96fa4

Please sign in to comment.