Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use multiple variables in Rust's for loop?

Tags:

rust

In the C family of languages, I can do this on one line:

for(int i = lo, int j = mid+1; i <= mid && j <= hi; i++, j++){
    ...
}

But in Rust... I can only write it like this:

for i in lo..mid+1 {
    let mut j = mid+1;
    if j <= hi {
        break;
    }
    ...
    j += 1;
}

Is there's a more efficient way to implement this?


Using an iterator works for above, but using an iterator makes some occasions like using arithmetic troublesome, such as

for (int i = 0; i < n; i ++) {
    if (a[i] == ...) {
        i += 5;
    }
}

In Rust, this does not work. The variable i will not be incremented by 5, but by 1 instead:

for i in 0..n {
    if a[i] == ... {
        i += 5;
    }
} 
like image 398
wind2412 Avatar asked Apr 09 '17 03:04

wind2412


1 Answers

You can create two parallel range iterators, zip them, then iterate though the combination:

fn main() {
    let values = [10, 20, 30, 40, 50, 60, 70, 80, 90];
    let lo = 2;
    let mid = 5;
    let hi = 7;

    let early_indexes = lo..(mid + 1);
    let late_indexes = (mid + 1)..(hi + 1);
    for (i, j) in early_indexes.zip(late_indexes) {
        println!("{}, {}", i, j);
        println!("{} - {}", values[i], values[j]);
    }
}

Someday, inclusive ranges will be stabilized, and you should be able to something like this (depending on the eventual syntax):

let early_indexes = lo...mid;
let late_indexes = (mid + 1)...hi;
for (i, j) in early_indexes.zip(late_indexes) {
    println!("{}, {}", i, j);
    println!("{} - {}", values[i], values[j]);
}

If you are actually iterating though a slice as I've shown for my example, you can also just combine the two iterators directly and ignore the index:

let early_values = values[lo..(mid + 1)].iter();
let late_values = values[(mid + 1)..(hi + 1)].iter();
for (i, j) in early_values.zip(late_values) {
    println!("{}, {}", i, j);
}

The variable i will not be incremented by 5, but by 1 instead.

Yes, incrementing by a step is annoying, and some day it will also be stabilized. In the meantime:

  • What is a stable way to iterate on a range with custom step?
  • How do I iterate over a range with a custom step?

If you need full control, you can always use while or loop:

let mut i = 0;
while i < n {
    if a[i] == ... {
        i += 5;
    }

    i += 1;
}
like image 69
Shepmaster Avatar answered Oct 06 '22 10:10

Shepmaster