Budding Rustacean here. I'm trying to update values inside a matrix based on some logic. The point is that I'm getting tripped up by the usize and i32 conflict that arises when indexing into my matrices row_counts
and col_counts
. The following is the error that keeps coming up:
error[E0277]: the type `[i32]` cannot be indexed by `&usize`
--> src/lib.rs:48:21
|
48 | row_counts[board_idx][r] += 1;
| ^^^^^^^^^^^^^^^^^^^^^^^^ slice indices are of type `usize` or ranges of `usize`
|
= help: the trait `SliceIndex<[i32]>` is not implemented for `&usize`
= note: required because of the requirements on the impl of `Index<&usize>` for `Vec<i32>`
error[E0277]: the type `[i32]` cannot be indexed by `&usize`
--> src/lib.rs:49:21
|
49 | col_counts[board_idx][c] += 1;
| ^^^^^^^^^^^^^^^^^^^^^^^^ slice indices are of type `usize` or ranges of `usize`
|
= help: the trait `SliceIndex<[i32]>` is not implemented for `&usize`
= note: required because of the requirements on the impl of `Index<&usize>` for `Vec<i32>`
error[E0277]: the type `[i32]` cannot be indexed by `&usize`
--> src/lib.rs:50:24
|
50 | if row_counts[board_idx][r] == 5 || col_counts[board_idx][c] == 5 {
| ^^^^^^^^^^^^^^^^^^^^^^^^ slice indices are of type `usize` or ranges of `usize`
|
= help: the trait `SliceIndex<[i32]>` is not implemented for `&usize`
= note: required because of the requirements on the impl of `Index<&usize>` for `Vec<i32>`
error[E0277]: the type `[i32]` cannot be indexed by `&usize`
--> src/lib.rs:50:57
|
50 | if row_counts[board_idx][r] == 5 || col_counts[board_idx][c] == 5 {
| ^^^^^^^^^^^^^^^^^^^^^^^^ slice indices are of type `usize` or ranges of `usize`
|
= help: the trait `SliceIndex<[i32]>` is not implemented for `&usize`
= note: required because of the requirements on the impl of `Index<&usize>` for `Vec<i32>`
For more information about this error, try `rustc --explain E0277`.
And this is the code that's causing this issue:
let num_boards = boards.len();
let mut row_counts: Vec<Vec<i32>> = vec![vec![0; 5]; num_boards]; // (num_boards x 5) matrix
let mut col_counts: Vec<Vec<i32>> = vec![vec![0; 5]; num_boards]; // (num_boards x 5) matrix
let (mut m1, mut m2) = (0, 0);
'outer: for draw in draws {
for (board_idx, board) in boards.iter().enumerate() {
match board.get(&draw) { // let mut boards: Vec<HashMap<i32, (usize, usize)>>;
Some((r, c)) => {
row_counts[board_idx][r] += 1;
col_counts[board_idx][c] += 1;
if row_counts[board_idx][r] == 5 || col_counts[board_idx][c] == 5 {
m1 = board_idx;
m2 = draw;
break 'outer;
}
}
None => {}
}
}
}
Any help to solve this issue is appreciated! (BONUS: Any resources / blogs / docs that I can read to avoid making the above errors would be even more awesome, because I don't even understand why this error is happening)