Loop Through Numbers In A Range In Rust
Start Your Engines
This is how I'm looking through a range of numbers in Rust:
Output:
1
2
3
4
5
6
7
8
9
10
Notes
-
The first number (i.e.
1
in this case) is inclusive. -
Using
..=
makes the second number (i.e.10
in this example) inclusive as well.
Not Inclusive
Use ..
instead of ..=
to make the second number exclusive. For example:
Output:
1
2
3
4
5
6
7
8
9
A Simple Note
I've been doing rust for a few years. Still had to look this up. It's not something I use all that often. Future me can use this not to remember faster.
-a
-- end of line --
Endnotes
There are a few other things you can do with ranges when you're using them for iterators over other things. See Rust Reference - Range Examples.