Reputation: 45116
This program compiles, but panics at run time with the message below. (playground)
use ndarray::prelude::*;
fn main() {
let a = Array2::<i32>::zeros([100, 10]);
let b = a.slice(s![5..15, ..8]);
let c = b.into_shape((10, 2, 4)).expect("should work");
dbg!(c);
}
thread 'main' panicked at 'should work: ShapeError/IncompatibleLayout: incompatible memory layout', src/main.rs:6:38
Reshaping fails because the view b
is not contiguous.
But this reshape is just splitting one axis in two, which can always be done safely. Analogous code works fine in NumPy:
a = np.arange(1000).reshape((100, 10))
b = a[5:15, :8]
c = b.reshape((10, 2, 4))
# array([[[ 50, 51, 52, 53],
# [ 54, 55, 56, 57]],
#
# [[ 60, 61, 62, 63],
# [ 64, 65, 66, 67]], ...])
How can I do that in Rust?
Upvotes: 0
Views: 250