@@ -467,6 +467,9 @@ where
467467
468468 /// Create an array with uninitalized elements, shape `shape`.
469469 ///
470+ /// Prefer to use [`maybe_uninit()`](ArrayBase::maybe_uninit) if possible, because it is
471+ /// easier to use correctly.
472+ ///
470473 /// **Panics** if the number of elements in `shape` would overflow isize.
471474 ///
472475 /// ### Safety
@@ -524,7 +527,66 @@ where
524527 S : DataOwned < Elem = MaybeUninit < A > > ,
525528 D : Dimension ,
526529{
527- pub ( crate ) fn maybe_uninit < Sh > ( shape : Sh ) -> Self
530+ /// Create an array with uninitalized elements, shape `shape`.
531+ ///
532+ /// The uninitialized elements of type `A` are represented by the type `MaybeUninit<A>`,
533+ /// an easier way to handle uninit values correctly.
534+ ///
535+ /// Only *when* the array is completely initialized with valid elements, can it be
536+ /// converted to an array of `A` elements using [`.assume_init()`].
537+ ///
538+ /// **Panics** if the number of elements in `shape` would overflow isize.
539+ ///
540+ /// ### Safety
541+ ///
542+ /// The whole of the array must be initialized before it is converted
543+ /// using [`.assume_init()`] or otherwise traversed.
544+ ///
545+ /// ### Examples
546+ ///
547+ /// It is possible to assign individual values through `*elt = MaybeUninit::new(value)`
548+ /// and so on.
549+ ///
550+ /// [`.assume_init()`]: ArrayBase::assume_init
551+ ///
552+ /// ```
553+ /// use ndarray::{s, Array2};
554+ /// use ndarray::Zip;
555+ /// use ndarray::Axis;
556+ ///
557+ /// // Example Task: Let's create a transposed copy of the input
558+ ///
559+ /// fn shift_by_two(a: &Array2<f32>) -> Array2<f32> {
560+ /// // create an uninitialized array
561+ /// let mut b = Array2::maybe_uninit(a.dim());
562+ ///
563+ /// // two first columns in b are two last in a
564+ /// // rest of columns in b are the initial columns in a
565+ ///
566+ /// assign_to(a.slice(s![.., -2..]), b.slice_mut(s![.., ..2]));
567+ /// assign_to(a.slice(s![.., 2..]), b.slice_mut(s![.., ..-2]));
568+ ///
569+ /// // Now we can promise that `b` is safe to use with all operations
570+ /// unsafe {
571+ /// b.assume_init()
572+ /// }
573+ /// }
574+ ///
575+ /// use ndarray::{IntoNdProducer, AssignElem};
576+ ///
577+ /// fn assign_to<'a, P1, P2, A>(from: P1, to: P2)
578+ /// where P1: IntoNdProducer<Item = &'a A>,
579+ /// P2: IntoNdProducer<Dim = P1::Dim>,
580+ /// P2::Item: AssignElem<A>,
581+ /// A: Clone + 'a
582+ /// {
583+ /// Zip::from(from)
584+ /// .apply_assign_into(to, A::clone);
585+ /// }
586+ ///
587+ /// # shift_by_two(&Array2::zeros((8, 8)));
588+ /// ```
589+ pub fn maybe_uninit < Sh > ( shape : Sh ) -> Self
528590 where
529591 Sh : ShapeBuilder < Dim = D > ,
530592 {
0 commit comments