Closed
Description
Consider this program:
fn main() {
let x = ~[1, 2, 3, 4, 5];
let mut i = x.len() - 1;
while i >= 0 {
if x[i] <= 3 {
io::println("At most three");
}
i -= 1;
}
}
The behaviour one might expect here is for the program to print "At most three" three times and then exiting. The actual program output is:
At most three
At most three
At most three
rust: task failed at 'index out of bounds: the len is 5 but the index
is -1', uint.rs:6
rust: domain main @0xdd9c10 root task failed
The problem is that i is unsigned, so the comparison i >= 0
will always be true. With the addition of "-Wtype-limits
" (GCC manual) this could easily be avoided.