4

I have a for loop in Rust and in a condition that is satisfied I want to rerun a part of the loop. For example:

for mut n in 0..vector.len(){
...
   if condition {
      n = 3;
  }
}

But in the next iteration n is not 3 and the loop continues. Any ideas?

2
  • 1
    n is not a reference from the iteration, it is just a copied value. even if you had succeed rust wouldn't allow this to prevent concurrent modification though. Commented Nov 27, 2021 at 17:05
  • I see. How would you suggest doing something like that? Commented Nov 27, 2021 at 17:12

1 Answer 1

6

for n in 0..x assigns to n values from 0 up to (but excluding) x without taking into account mutations to n, if they occur. That's the case in other languages with foreach-style iteration, e.g. in Python with for n in range(len(vector)), or in C++20 with for (auto n: std::views::iota(0, vector.size())).

If you need to occasionally return to a previous state, as afforded by C's for (n = 0; n < x; n++), the simplest option is to just code the C-style loop:

let mut n = 0;
while n < vector.len() {
    ...
    if condition {
        n = 3;
        continue;
    }
    n += 1;
}

The downside is that n += 1 is buried at the end of the loop, so that a continue that just wants to keep looping (without resetting the loop counter) would actually fail to increment n. If that bothers you (and it'd bother me), you can switch to a slightly different foreach-based implementation, where the range is explicit. Instead of with for, you loop over the range with while let. That gives you the robustness of foreach while allowing you to modify the range you're looping over from inside the loop:

let mut range = 0..vector.len();
while let Some(n) = range.next() {
    ...
    if condition {
        range = 3..vector.len();
    }
}

Note that your original code evaluated vector.len() only once. The C-style loop checks it every time, and this last example also checks it when condition is true. If you change the size of the vector, you should probably accompany vector.push(...) by range = n..vector.len() to correct the upper bound.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.