我对i
下面的变量做错了什么?为什么编译器说我不能Vec
用 a索引 au32
以及如何修复它?
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[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>`