I'm looking at some code that looks like this:
for mut i in 2..points.len() {
// Do something
if some_condition {
i += 1;
}
}
It compiles and runs fine (it seems). My first intuition was that it would be better as a while loop.
Then I started wondering, is this something that's legal to do in Rust in general?
What happens if you happen to increment i beyond the range? I'm guessing trouble obviously...
I'd like to know if there are any kind of issues modifying the index while going through the range with it. I'm assuming that the i stays in range, but I would be interested to know what happens when it goes beyond the range too.
iis not an index, its a value from the range you generated with2..points.len(). You can change and modifyias much as you want, it will just be reassigned for the next iteration. If you tried to take a value from points withpoints[i]you may get an index error, but just modifying it on its own is no issue (as long as you don't exceed the type limits)