Skip to content

Commit 66c9d2b

Browse files
committed
DOC: Update Array::uninitialized's example and doc
See the bug reports; these examples can not really be recommended; instead we recommend using Array::maybe_uninit.
1 parent d9ea1e1 commit 66c9d2b

File tree

1 file changed

+8
-29
lines changed

1 file changed

+8
-29
lines changed

src/impl_constructors.rs

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -474,41 +474,20 @@ where
474474
///
475475
/// ### Safety
476476
///
477-
/// Accessing uninitalized values is undefined behaviour. You must
478-
/// overwrite *all* the elements in the array after it is created; for
479-
/// example using the methods `.fill()` or `.assign()`.
477+
/// Accessing uninitalized values is undefined behaviour. You must overwrite *all* the elements
478+
/// in the array after it is created; for example using
479+
/// [`raw_view_mut`](ArrayBase::raw_view_mut) or other low-level element access.
480480
///
481481
/// The contents of the array is indeterminate before initialization and it
482482
/// is an error to perform operations that use the previous values. For
483483
/// example it would not be legal to use `a += 1.;` on such an array.
484484
///
485485
/// This constructor is limited to elements where `A: Copy` (no destructors)
486-
/// to avoid users shooting themselves too hard in the foot; it is not
487-
/// a problem to drop an array created with this method even before elements
488-
/// are initialized. (Note that constructors `from_shape_vec` and
489-
/// `from_shape_vec_unchecked` allow the user yet more control).
490-
///
491-
/// ### Examples
492-
///
493-
/// ```
494-
/// use ndarray::{s, Array2};
495-
///
496-
/// // Example Task: Let's create a column shifted copy of a in b
497-
///
498-
/// fn shift_by_two(a: &Array2<f32>) -> Array2<f32> {
499-
/// let mut b = unsafe { Array2::uninitialized(a.dim()) };
500-
///
501-
/// // two first columns in b are two last in a
502-
/// // rest of columns in b are the initial columns in a
503-
/// b.slice_mut(s![.., ..2]).assign(&a.slice(s![.., -2..]));
504-
/// b.slice_mut(s![.., 2..]).assign(&a.slice(s![.., ..-2]));
505-
///
506-
/// // `b` is safe to use with all operations at this point
507-
/// b
508-
/// }
509-
///
510-
/// # shift_by_two(&Array2::zeros((8, 8)));
511-
/// ```
486+
/// to avoid users shooting themselves too hard in the foot.
487+
///
488+
/// (Also note that the constructors `from_shape_vec` and
489+
/// `from_shape_vec_unchecked` allow the user yet more control, in the sense
490+
/// that Arrays can be created from arbitrary vectors.)
512491
pub unsafe fn uninitialized<Sh>(shape: Sh) -> Self
513492
where
514493
A: Copy,

0 commit comments

Comments
 (0)