Skip to content

Instantly share code, notes, and snippets.

@mgdm
Last active May 2, 2016 22:18
Show Gist options
  • Save mgdm/b0cef42efc388c2a6c241c202124cf02 to your computer and use it in GitHub Desktop.
Save mgdm/b0cef42efc388c2a6c241c202124cf02 to your computer and use it in GitHub Desktop.
Inclusive ranges in Rust

I was writing some code in Rust 1.8.0, which needed to iterate through all the values of a u8. My first attempt looked like this:

fn main() {
    for i in 0u8..255 {
        println!("Iteration {}", i);
    }
}

The output stops at 254:

michael@morbo:ranges% ./test1                                                                                                                                          22:45:18
Iteration 0
...
Iteration 254

OK, so ranges aren't inclusive. Fine, I can fix this. My next attempt:

fn main() {
    for i in 0u8..256 {
        println!("Iteration {}", i);
    }
}

When compiling, it threw this warning:

michael@morbo:ranges% rustc test2.rs                                                                                                                                   22:48:10
test2.rs:2:19: 2:22 warning: literal out of range for u8, #[warn(overflowing_literals)] on by default
test2.rs:2     for i in 0u8..256 {
                             ^~~

And running it produced no output, presumably because 256 wrapped around to 0 resulting in an empty range.

Hmmmm.

Then I read about range_inclusive, so decided to give that a go.

use std::iter::range_inclusive;

fn main() {
    for i in range_inclusive(0u8, 255) {
        println!("Iteration {}", i);
    }
}
michael@morbo:ranges% rustc test3.rs                                                                                                                                   22:49:58
test3.rs:1:5: 1:31 error: use of unstable library feature 'range_inclusive': likely to be replaced by range notation and adapters (see issue #27777)
test3.rs:1 use std::iter::range_inclusive;
               ^~~~~~~~~~~~~~~~~~~~~~~~~~
test3.rs:4:14: 4:29 error: use of unstable library feature 'range_inclusive': likely to be replaced by range notation and adapters (see issue #27777)
test3.rs:4     for i in range_inclusive(0u8, 255) {
                        ^~~~~~~~~~~~~~~
test3.rs:1:5: 1:31 warning: use of deprecated item: replaced with ... syntax, #[warn(deprecated)] on by default
test3.rs:1 use std::iter::range_inclusive;
               ^~~~~~~~~~~~~~~~~~~~~~~~~~
test3.rs:4:14: 4:29 warning: use of deprecated item: replaced with ... syntax, #[warn(deprecated)] on by default
test3.rs:4     for i in range_inclusive(0u8, 255) {
                        ^~~~~~~~~~~~~~~
error: aborting due to 2 previous errors

I went off to try and find out how to enable this "unstable" feature. It turns out that I can't, because I'm using a stable version of Rust. I'd need to recompile from source to make that work.

Fine. That's been deprecated, but it's told me what its replacement is, so let's try that.

fn main() {
    for i in 0u8...255 {
        println!("Iteration {}", i);
    }
}
michael@morbo:ranges% rustc test4.rs                                                                                                                                   22:55:49
test4.rs:2:17: 2:20 error: expected one of `.`, `{`, or an operator, found `...`
test4.rs:2     for i in 0u8...255 {
                           ^~~
error: aborting due to previous error

Oh. So the range_inclusive feature has been deprecated or made unstable (how can it be both?) and its replacement hasn't been implemented yet.

Hmmm.

In the end I made the range a u16 and cast it back to u8 when I needed it.

fn main() {
    for i in 0u16..256 {
        println!("Iteration {}", i as u8);
    }
}

And that works!

michael@morbo:ranges% ./test5                                                                                                                                          22:58:22
Iteration 0
...
Iteration 255

On the whole I like the language so far, but it has sent me off on a wild goose chase a couple of times already.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment