..= for inclusive ranges
Since well before Rust 1.0, you’ve been able to create exclusive ranges with..
like this:
for i in 1..3 {
println!("i: {}", i);
}
This will print i: 1
and then i: 2
. Today, you can now create aninclusive range, like this:
#![allow(unused_variables)]
fn main() {
for i in 1..=3 {
println!("i: {}", i);
}
}
This will print i: 1
and then i: 2
like before, but also i: 3
; thethree is included in the range. Inclusive ranges are especially useful if youwant to iterate over every possible value in a range. For example, this is asurprising Rust program:
fn takes_u8(x: u8) {
// ...
}
fn main() {
for i in 0..256 {
println!("i: {}", i);
takes_u8(i);
}
}
What does this program do? The answer: it fails to compile. The error we getwhen compiling has a hint:
error: literal out of range for u8
--> src/main.rs:6:17
|
6 | for i in 0..256 {
| ^^^
|
= note: #[deny(overflowing_literals)] on by default
That’s right, since i
is a u8
, this overflows, and the compiler producesan error.
We can do this with inclusive ranges, however:
fn takes_u8(x: u8) {
// ...
}
fn main() {
for i in 0..=255 {
println!("i: {}", i);
takes_u8(i);
}
}
This will produce those 256 lines of output you might have been expecting.