Using a matrix (multidimensional fixed sized array) defined as [[f64; 4]; 4], is it possible to swap two values?
std::mem::swap(&mut matrix[i][k], &mut matrix[k][l]);
Gives the error:
error[E0499]: cannot borrow `matrix[..][..]` as mutable more than once at a time
--> math_matrix.rs:100
|
100 | std::mem::swap(&mut matrix[i][j], &mut matrix[k][l]);
| ------------ ^^^^^^^^^^^^^^^- first borrow ends here
| | |
| | second mutable borrow occurs here
| first mutable borrow occurs here
The only way I could figure out how to accomplish this was to use a temp value, e.g.:
macro_rules! swap_value {
($a_ref:expr, $b_ref:expr) => {
{
let t = *$a_ref;
*$a_ref = *$b_ref;
*$b_ref = t;
}
}
}
Then use:
swap_value!(&mut matrix[i][k], &mut matrix[maxj][k]);
Is there a better alternative?