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