I have a code that counts number of elements in vector that contains only 0, 1, 2 values
pub fn sort_colors(nums: &mut Vec<i32>) {
let mut cntColors = vec![0; 3];
for num in nums {
cntColors[(num) as usize] += 1; // ERROR: casting `&mut i32` as `usize` is invalid
}
I fixed the error with help of this code
for idx in 0..nums.len() {
cntColors[nums[idx] as usize] += 1;
}
What is the proper way to cast &mut i32
to usize
?