mcandre
mcandre

Reputation: 24612

How do you make a range in Rust?

The documentation doesn't say how and the tutorial completely ignores for loops.

Upvotes: 53

Views: 76624

Answers (7)

xiaguangbo
xiaguangbo

Reputation: 115

range in fn:

fn print_range(range: std::ops::Range<i32>) {
    for num in range {
        println!("{}", num);
    }
}

fn main() {
    let my_range = 1..=5;
    print_range(my_range);
}

Upvotes: 0

troglodytto
troglodytto

Reputation: 121

let range = (start..end).collect::<Vec<i32>>();

Upvotes: 6

confused00
confused00

Reputation: 2612

for i in range(0, 100) is now deprecated in favour of for i in 0..100 (according to rustc 1.0.0-nightly.

Also worth noting is the compiler can't disambiguate when you use an identifier in the range (e.g., for i in 0..a), so you have to use for i in (0..a), but there's a pull request submitted to fix this.

Upvotes: 12

nejucomo
nejucomo

Reputation: 534

As of 1.0, for loops work with values of types with the Iterator trait.

The book describes this technique in chapter 3.5 and chapter 13.2.

If you are interested in how for loops operate, see the described syntactic sugar in Module std::iter.

Example:

fn main() {
    let strs = ["red", "green", "blue"];

    for sptr in strs.iter() {
        println!("{}", sptr);
    }
}

(Playground)

If you just want to iterate over a range of numbers, as in C's for loops, you can create a numeric range with the a..b syntax:

for i in 0..3 {
    println!("{}", i);
}

If you need both, the index and the element from an array, the idiomatic way to get that is with the Iterator::enumerate method:

fn main() {
    let strs = ["red", "green", "blue"];

    for (i, s) in strs.iter().enumerate() {
        println!("String #{} is {}", i, s);
    }
}

Notes:

  • The loop items are borrowed references to the iteratee elements. In this case, the elements of strs have type &'static str - they are borrowed pointers to static strings. This means sptr has type &&'static str, so we dereference it as *sptr. An alternative form which I prefer is:

      for &s in strs.iter() {
          println!("{}", s);
      }
    

Upvotes: 45

Lindsey Kuper
Lindsey Kuper

Reputation: 984

Actually, the Loops section of the tutorial does cover for loops:

When iterating over a vector, use for instead.

for elt in ["red", "green", "blue"] {
   std::io::println(elt);
}

But if you needed indices, you could do something like the following, using the uint::range function from the core library (or int::range or u8::range or u32::range or u64::range) and Rust's syntax for blocks:

range(0u, 64u, {|i| C[i] = A[i] + B[i]});

Rust used to support this equivalent syntax but it was later removed:

range(0u, 64u) {|i|
    C[i] = A[i] + B[i];
}

Upvotes: 10

Bob Hansen
Bob Hansen

Reputation: 25

Note that as of rustc 0.4 (Oct 2012), the alternate construction of

range(0u, 64u) {|i|
    C[i] = A[i] + B[i];
}

appears to not be supported any more.

Upvotes: 0

mcandre
mcandre

Reputation: 24612

Use int::range.

Upvotes: -4

Related Questions