To iterate over an array in Rust, you can use iterators or a for
loop.
In this example,
numbers
with some values.for
loop to iterate over each element of the array and print the elements.fn main() {
let numbers = [1, 2, 3, 4, 5];
for num in &numbers {
println!("{}", num);
}
}
1 2 3 4 5
In this example,
fruits
with some values.fn main() {
let fruits = ["apple", "banana", "cherry"];
for fruit in fruits.iter() {
println!("{}", fruit);
}
}
apple banana cherry
In this tutorial, we learned How to Iterate Over an Array in Rust language with well detailed examples.