3 ms·
The problem is not breaking before an iteration has completed, it's unreachable code. In your example, you have no unreachable code. However, move that print
by jnbiche 12y ago
The problem is not breaking before an iteration has completed, it's unreachable code. In your example, you have no unreachable code.
However, move that print statement to the line below your break, and you'll see that Rust gives an 'unreachable code' warning.
let l = [1i, 2, 3];
for i in l.iter() {
println!("{}", i);
break;
}
And I'm pretty sure it can be configured to not compile at all on warning. Can someone with the project confirm?
- simias 12y agoYou forgot to update the snippet but I get what you mean, but that's not the problem in the C code in TFA, the return is the last operation in the for loop (no code after it). But you are correct that rust does emit a warning if you put the println! after the break while gcc 4.9.1 builds the following without any warning even with -Wextra: #include <stdio.h> int main() { int i; for (i = 0; i < 10; i++) { break; printf("unreached\n"); } return 0; } Which is pretty surprising to me, especially since the optimizer is clever enough to completely remove the for loop and the static string from the asm output, so unlike rust they actually know it's unused.