for loop
Language: Rust - for loop
In Rust, the for
loop is used to iterate over a collection of items, such as an array, vector, or range. Here is the basic syntax of a for
loop in Rust:
for item in collection {
// code to be executed for each item
}
item
is a variable that represents each item in the collection.collection
is the collection of items to iterate over.
During each iteration of the loop, the code inside the loop block will be executed, and the item
variable will hold the current item from the collection.
Here's an example of using a for
loop to iterate over an array in Rust:
fn main() {
let numbers = [1, 2, 3, 4, 5];
for number in numbers.iter() {
println!("Number: {}", number);
}
}
This code will print each number in the numbers
array.
Note: The iter()
method is used to create an iterator over the array's elements.
That's the basic usage of the for
loop in Rust. Let me know if you need further assistance!