To iterate over an array in Rust in reverse order, you can use a for
loop starting from the last index to the first index.
In this example,
numbers
with some values.for
loop starting from the last index to iterate over each element of the array in reverse order.fn main() {
let numbers = [1, 2, 3, 4, 5];
for i in (0..numbers.len()).rev() {
println!("{}", numbers[i]);
}
}
5 4 3 2 1
In this example,
fruits
with some values.for
loop starting from the last index to iterate over each element of the array in reverse order.fn main() {
let fruits = vec!["apple", "banana", "cherry"];
for i in (0..fruits.len()).rev() {
println!("{}", fruits[i]);
}
}
cherry banana apple
In this tutorial, we learned How to Iterate Over an Array in Reverse Order in Rust language with well detailed examples.