I can't get this code to compile:
fn main() {
let grid: [[Option<i32>;2];2] = [
[Some(1),Some(2)],
[None,Some(4)],
];
for row in grid.iter() {
for v in row.iter() {
match v {
Some(x) => print!("{}", x),
None => print!(" "),
}
}
print!("\n");
}
}
I get this error message
Compiling array-2d v0.1.0 (file:///Users/paul/src/test/rust/array-2d)
src/main.rs:8:5: 13:6 error: type mismatch resolving `<core::slice::Iter<'_, core::option::Option<i32>> as core::iter::Iterator>::Item == core::option::Option<_>`:
expected &-ptr,
found enum `core::option::Option` [E0271]
src/main.rs: 8 for v in row.iter() {
src/main.rs: 9 match v {
src/main.rs:10 Some(x) => print!("{}", x),
src/main.rs:11 None => print!(" "),
src/main.rs:12 }
src/main.rs:13 }
src/main.rs:8:5: 13:6 note: in this expansion of for loop expansion
src/main.rs:7:3: 15:4 note: in this expansion of for loop expansion
src/main.rs:8:5: 13:6 help: run `rustc --explain E0271` to see a detailed explanation
error: aborting due to previous error
Could not compile `array-2d`.
Can someone interpret that to tell me what I'm doing wrong?
for x in &collection. Example.for row in &gridlets me leave off the.iter()?forloop expression must adhere toIntoIterator, which is automatically called. You'll note thatIntoIteratoris implemented for references to arrays and slices.IntoIteratoris used forforloops.Tbut not forTitself? That is a new concept to me. Really?