rustRust for loop range inclusive example
A for
loop in Rust can be used to iterate over a range of values. The range can be inclusive or exclusive. An example of an inclusive range is shown below:
for i in 0..=5 {
println!("{}", i);
}
This code will output the following:
0
1
2
3
4
5
The code consists of the following parts:
for
: This is the keyword used to start a loop.i
: This is the variable used to store the current value of the loop.0..=5
: This is the range of values that the loop will iterate over. The..=
indicates that the range is inclusive.println!("{}", i);
: This is the code that will be executed for each iteration of the loop. It prints the current value ofi
to the console.
Helpful links
Related
More of Rust
- Hashshet example in Rust
- How to convert Rust bytes to hex?
- How to convert a Rust HashMap to a JSON string?
- How to convert a Rust HashMap to JSON?
- How to get the last element of a Rust slice?
- How to use non-capturing groups in Rust regex?
- How to use groups in a Rust regex?
- How to match the end of a line in a Rust regex?
- How to escape dots with regex in Rust?
- How to use regex to match a group in Rust?
See more codes...