Hello everyone
I was hoping someone can explain to me what is happening in this example. I am creating a larger and a smaller 2D array and I am trying to add_assign()
the smaller array to the center of the larger array.
Why does Variant 1
not work but all the others do?
use ndarray::prelude::*;
use std::ops::AddAssign;
fn main() {
let mut a: Array2<i32> = Array2::zeros([4, 4]);
let b = Array2::ones([2, 2]);
let mask = s![1..3, 1..3];
// Variant 1
// a.slice_mut(mask) += &b;
// println!("Variant 1\n{}\n", a);
// Variant 2
let mut slice = a.slice_mut(mask);
slice += &b;
println!("Variant 2\n{}\n", a);
// Variant 3
a.slice_mut(mask).add_assign(&b);
println!("Variant 3\n{}\n", a);
// Variant 4
*&mut a.slice_mut(mask) += &b;
println!("Variant 4\n{}\n", a);
}
Output:
Variant 2
[[0, 0, 0, 0],
[0, 1, 1, 0],
[0, 1, 1, 0],
[0, 0, 0, 0]]
Variant 3
[[0, 0, 0, 0],
[0, 2, 2, 0],
[0, 2, 2, 0],
[0, 0, 0, 0]]
Variant 4
[[0, 0, 0, 0],
[0, 3, 3, 0],
[0, 3, 3, 0],
[0, 0, 0, 0]]
Errors:
Compiling playground v0.0.1 (/playground)
Finished dev [unoptimized + debuginfo] target(s) in 1.04s
Running `target/debug/playground`
Errors when uncommenting 1:
error[E0067]: invalid left-hand side of assignment
--> src/main.rs:11:23
|
11 | a.slice_mut(mask) += &b;
| ----------------- ^^
| |
| cannot assign to this expression