What am I doing wrong with the variable i
below? Why does the compiler say I cannot index a Vec
with a u32
and how do I fix it?
fn main() {
let a: Vec<u32> = vec![1, 2, 3, 4];
let number: u32 = 4;
let mut count = 0;
for i in 0..number {
if a[i] % 2 != 0 {
count += 1;
} else {
continue;
}
}
println!("{}", count);
}
Error:
error[E0277]: the type `[u32]` cannot be indexed by `u32`
--> src/main.rs:7:12
|
7 | if a[i] % 2 != 0 {
| ^^^^ slice indices are of type `usize` or ranges of `usize`
|
= help: the trait `SliceIndex<[u32]>` is not implemented for `u32`
= note: required because of the requirements on the impl of `Index<u32>` for `Vec<u32>`