Skip to content

Commit d2ba2e7

Browse files
committed
Add note about memory layout in .to_owned() docs
1 parent 9f18d6f commit d2ba2e7

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

src/impl_methods.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,35 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
124124
}
125125
}
126126

127-
/// Return an uniquely owned copy of the array
127+
/// Return an uniquely owned copy of the array.
128+
///
129+
/// For performance, the memory layout of the resulting array is arbitrary.
130+
/// If you need a particular layout, you can allocate a new array with the
131+
/// desired memory layout and [`.assign()`](#method.assign) the data.
132+
/// Alternatively, you can collect an iterator, like this for a result in
133+
/// standard layout:
134+
///
135+
/// ```
136+
/// # use ndarray::prelude::*;
137+
/// # let arr = Array::from_shape_vec((2, 2).f(), vec![1, 2, 3, 4]).unwrap();
138+
/// # let owned = {
139+
/// Array::from_shape_vec(arr.raw_dim(), arr.iter().cloned().collect()).unwrap()
140+
/// # };
141+
/// # assert!(owned.is_standard_layout());
142+
/// # assert_eq!(arr, owned);
143+
/// ```
144+
///
145+
/// or this for a result in column-major (Fortran) layout:
146+
///
147+
/// ```
148+
/// # use ndarray::prelude::*;
149+
/// # let arr = Array::from_shape_vec((2, 2), vec![1, 2, 3, 4]).unwrap();
150+
/// # let owned = {
151+
/// Array::from_shape_vec(arr.raw_dim().f(), arr.t().iter().cloned().collect()).unwrap()
152+
/// # };
153+
/// # assert!(owned.t().is_standard_layout());
154+
/// # assert_eq!(arr, owned);
155+
/// ```
128156
pub fn to_owned(&self) -> Array<A, D>
129157
where A: Clone
130158
{

0 commit comments

Comments
 (0)