rustRust negative for loop example
Negative for loops in Rust are used to iterate over a range of numbers in reverse order. The syntax for a negative for loop is similar to a regular for loop, but the range is specified with a .rev()
method.
Example code
for i in (0..10).rev() {
println!("{}", i);
}
Output example
9
8
7
6
5
4
3
2
1
0
Code explanation
for i in (0..10).rev()
: This is the loop syntax, which specifies the range of numbers to iterate over. The.rev()
method reverses the range, so the loop will start at 10 and end at 0.println!("{}", i)
: This is the code that will be executed for each iteration of the loop. In this case, 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...