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
- How to loop until error in Rust
- How to sleep in a loop in Rust
- Rust parallel loop example
- How to iterate a map in Rust
- How to do a for loop with index in Rust
- How to iterate linked list in Rust
- How to iterate string lines in Rust
- How to iterate in pairs in Rust
- How to iterate through hashmap keys in Rust
- How to continue loop in Rust
More of Rust
- How to match a URL with a regex in Rust?
- How to match whitespace with a regex in Rust?
- How to replace a capture group using Rust regex?
- How to convert struct to JSON string in Rust?
- How to get an entry from a HashSet in Rust?
- How to create a slice from a string in Rust?
- How do I print the type of a variable in Rust?
- How to write struct to file in Rust
- Pointer comparison in Rust
- How to replace all matches using Rust regex?
See more codes...