rustLoop counter example in Rust
Loop counter example in Rust is a way to iterate over a collection of items and perform an action on each item. The most common way to do this is with a for
loop. The for
loop takes a variable, called a counter, and assigns it a value from the collection. The loop then executes a block of code for each value of the counter.
for counter in 0..10 {
println!("Counter is {}", counter);
}
Output example
Counter is 0
Counter is 1
Counter is 2
Counter is 3
Counter is 4
Counter is 5
Counter is 6
Counter is 7
Counter is 8
Counter is 9
Code explanation
for
loop: the loop that iterates over the collection of itemscounter
: the variable that is assigned a value from the collection0..10
: the range of values that the counter will takeprintln!
: the function that prints out the value of the counter
Helpful links
Related
- How to loop until error in Rust
- How to iterate linked list in Rust
- How to iterate over string in Rust
- How to iterate a map in Rust
- Rust for loop range inclusive example
- How to do a for loop with index in Rust
- How to iterate in pairs in Rust
- How to loop N times in Rust
- Rust negative for loop example
- Rust parallel loop example
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...