The code below is built successfully with rust 1.43.0 in Rust Playground.
// success
fn main() {
let mut numbers: [Option<u16>; 5] = [Some(0); 5];
for iter in 0..5 {
let number_to_add: u16 = 1;
numbers[iter] = Some(number_to_add);
}
}
After a little change, it built failed.
// failed
fn main() {
let mut numbers: [Option<u16>; 5] = [Some(0); 5];
for iter in 0..5 {
let number_to_add: u16 = iter * 1; // changed
numbers[iter] = Some(number_to_add);
}
}
with error:
Compiling playground v0.0.1 (/playground)
error[E0277]: the type `[std::option::Option<u16>]` cannot be indexed by `u16`
--> src/main.rs:6:9
|
6 | numbers[iter] = Some(number_to_add);
| ^^^^^^^^^^^^^ slice indices are of type `usize` or ranges of `usize`
|
= help: the trait `std::slice::SliceIndex<[std::option::Option<u16>]>` is not implemented for `u16`
= note: required because of the requirements on the impl of `std::ops::Index<u16>` for `[std::option::Option<u16>]`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground`.
To learn more, run the command again with --verbose.
The numbers[iter] = Some(number_to_add);
line didn't change either way.