Why isn't this working? I assume it has to do with the fact that I'm taking two Box<[&T]> and outputting a Box<[T]>, but wouldn't it automatically perform dereferencing if T has the Copy trait?
but wouldn't it automatically perform dereferencing if T has the Copy trait?
Rust doesn't automatically dereference. Also, automatic reference won't be applied for operator arguments.
The standard library implements operators for reference types e.g. &i32 + &i32, &i32 + i32 and i32 + &i32 in addition to i32 + i32, but this is done by manual implementations (actually, macro is used).
Because you defined only &Matrix + &Matrix, arguments of the plus operator have to be referenced:
println!("{:?}", &a + &b);
However, the code still doesn't compile. This is because the type of a and b is Matrix<&i32>. You are passing &[i32; 4] to Matrix::new. It is a IntoIterator but with Item = &i32. This issue can be solved by copying iterator elements:
let a = Matrix::new([2, 1, -2, 4].iter().copied(), 2);
let b = Matrix::new([2, 1, -2, 4].iter().copied(), 2);
Probably you are misunderstanding about reference and owned values. Matrix<&i32> means a matrix of references (a pointer). Which is likely not what you want.
Values being referenced has to be owned by someone. The result of addition has to be an owned value.
The only reason this compiles is that the blah() fn call will automatically deref (aka autoderef) the provided foo value in order to arrive at the necessary &str type.
That is true but what I meant is there is no conversion of &X to X. Deref conversion is always &X to &Y and this is (I think calling it deref conversion is confusing).