I tried to execute the following code snippet:
let a = &[Some(1), Some(2), Some(3), None, Some(4)];
let mut sum = 0;
for &Some(x) in a.iter() {
sum += x;
}
assert_eq!(sum, 1+2+3+4);
The compiler replied:
about_loops.rs:39:9: 43:18 error: non-exhaustive patterns: None not covered
about_loops.rs:39 for &Some(x) in a.iter() {
about_loops.rs:40 sum += x;
about_loops.rs:41 }
about_loops.rs:42
about_loops.rs:43 assert_eq!(sum, 1+2+3+4);
error: aborting due to previous error
make: *** [all] Error 101
Can I make such a compiler for the for loop without using the matching expression suggested by luke and hobbs? Or is this error message misleading? This does not seem to be given by the grammatical definition of for.
for_expr : "for" pat "in" expr '{' block '}' ;
I am:
rustc 0.11.0-pre-nightly (6291955 2014-05-19 23:41:20 -0700)
host: x86_64-apple-darwin
To clarify: how expressive is the "pat" part for_expr? This is not specified in http://doc.rust-lang.org/rust.html#for-expressions , unlike the definition in http://doc.rust-lang.org/rust.html#match-expressions .
source
share