rustRust loop example
A for
loop is a common type of loop in Rust. It allows you to iterate over a collection of items, such as an array or a vector.
let numbers = [1, 2, 3, 4, 5];
for number in numbers.iter() {
println!("{}", number);
}
Output example
1
2
3
4
5
The code above will loop through the numbers
array and print out each element. The iter()
method is used to get an iterator over the array. The for
loop will then loop through each element in the iterator and print it out.
Code explanation
let numbers = [1, 2, 3, 4, 5];
: This line declares an array of numbers.for number in numbers.iter()
: This line starts thefor
loop. It will loop through each element in thenumbers
array.println!("{}", number);
: This line prints out the current element in the loop.
Helpful links
Related
More of Rust
- How do I identify unused variables in Rust?
- How to parse JSON string in Rust?
- How to use a tuple as a key in a Rust HashMap?
- How to replace a capture group using Rust regex?
- How to get a capture group using Rust regex?
- How to convert the keys of a Rust HashMap to a vector?
- How to get the length of a Rust HashMap?
- How to replace strings using Rust regex?
- How to use non-capturing groups in Rust regex?
- How to use a HashBrown with a Rust HashMap?
See more codes...