// don't ask me why I'm doing that, the real algorithm is slightly
// more complicated!
let mut matrix: Vec<Vec<bool>> = ...;
for i in 0..5 {
for j in 0..5 {
if i == j {
continue;
}
// this works
for k in 0..5 {
matrix[i][k] |= matrix[j][k];
}
// this doesn't work
// or_assign(&mut matrix[i], &matrix[j]);
}
}
The borrow checker doesn't allow me to borrow both mutably matrix[i]
and immutably matrix[j]
. In the general case I would totally agree. However, i != j
, so the borrow checker is wrong. The issue is that in my real code I'm using a Vec<BitVec>
, so I can't manually use the innermost loops, but instead I want to use the or_assign
function.
Is this the right way to tell the borrow checker that he is wrong, or is there a cleaner way?
// SAFETY: This works because i != j
let left: *mut _ = &mut matrix[i];
let left = unsafe { &mut *left };
let right = &matrix[i];
or_assign(left, right);