使用一个被定义为
给出以下错误提示:
唯一我能想到的实现方式是使用一个临时值,例如:
有更好的替代品吗?
[[f64; 4]; 4]
的矩阵(多维定长数组),是否可以交换两个值?std::mem::swap(&mut matrix[i][k], &mut matrix[k][l]);
给出以下错误提示:
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
唯一我能想到的实现方式是使用一个临时值,例如:
macro_rules! swap_value {
($a_ref:expr, $b_ref:expr) => {
{
let t = *$a_ref;
*$a_ref = *$b_ref;
*$b_ref = t;
}
}
}
然后使用:
swap_value!(&mut matrix[i][k], &mut matrix[maxj][k]);
有更好的替代品吗?
y
的索引。这是一个容易犯错的地方。 - Matthieu M.