Dear all,
I need help please. I'm using an ndarray
crate. I would like to copy or clone a slice of an Array3
. Given the following arrays:
// initialise electric field values Ex
let mut ex = Array3::<f32>::zeros((NX, NY, NZ));
// electric field values @ boundary planes @ time step m=n-1
let mut ex_jmin_m = Array3::<f32>::zeros((NX,2,NZ));
I can do it the explicit loop way:
for i in IMIN+1..IMAX {
for j in JMIN+1..JMAX {
// Ex @ jmin, jmax
ex_jmin_m[[i,0,k]] = ex[[i,JMIN+1,k]];
ex_jmin_m[[i,1,k]] = ex[[i,JMIN+2,k]];
}
}
But how do I do it in an idiomatic Rust (rustic/rusty way)? I tried using slices and clone()
:
error[E0599]: the method `clone` exists for struct `ArrayBase<ViewRepr<&mut f32>, Dim<[usize; 3]>>`, but its trait bounds were not satisfied
--> src/main.rs:164:79
|
164 | ex_jmin_m = ex.slice_mut(s![IMIN+1..IMIN, JMIN+1..=JMIN+2, KMIN+1..KMAX]).clone();
| ^^^^^ method cannot be called on `ArrayBase<ViewRepr<&mut f32>, Dim<[usize; 3]>>` due to unsatisfied trait bounds
|
::: /Users/mabalenk/.cargo/registry/src/index.crates.io-6f17d22bba15001f/ndarray-0.15.6/src/lib.rs:1268:1
|
1268 | pub struct ArrayBase<S, D>
| -------------------------- doesn't satisfy `_: Clone`
...
1459 | pub struct ViewRepr<A> {
| ---------------------- doesn't satisfy `ViewRepr<&mut f32>: RawDataClone`
|
= note: the following trait bounds were not satisfied:
`ViewRepr<&mut f32>: RawDataClone`
which is required by `ArrayBase<ViewRepr<&mut f32>, Dim<[usize; 3]>>: Clone`
I attempted using slices and assign_to()
:
ex.slice(s![IMIN+1..IMIN, JMIN+1..=JMIN+2, KMIN+1..KMAX]).assign_to(ex_jmin_m);
println!("shape(ex_jmin_m): {:?}", &ex_jmin_m.shape());
But the compilation fails with the following error:
error[E0382]: borrow of moved value: `ex_jmin_m`
--> src/main.rs:160:40
|
133 | ex_jmin_m: &mut Array3<f32>, ex_jmax_m: &mut Array3<f32>,
| --------- move occurs because `ex_jmin_m` has type `&mut ArrayBase<OwnedRepr<f32>, Dim<[usize; 3]>>`, which does not implement the `Copy` trait
...
159 | ex.slice(s![IMIN+1..IMIN, JMIN+1..=JMIN+2, KMIN+1..KMAX]).assign_to(ex_jmin_m);
| --------- value moved here
160 | println!("shape(ex_jmin_m): {:?}", ex_jmin_m.shape());
| ^^^^^^^^^^^^^^^^^ value borrowed here after move