To reverse an array in Rust, you can use the slice method reverse() on mutable slices.
In this example,
int_array
containing integers.reverse()
method on the mutable slice of int_array
to reverse the elements in place.fn main() {
let mut int_array = [1, 2, 3, 4, 5];
int_array.reverse();
println!("Reversed Array: {:?}", int_array);
}
Reversed Array: [5, 4, 3, 2, 1]
In this example,
str_array
containing strings.reverse()
method on the mutable slice of str_array
to reverse the elements in place.fn main() {
let mut str_array = ["apple", "banana", "cherry"];
str_array.reverse();
println!("Reversed Array: {:?}", str_array);
}
Reversed Array: ["cherry", "banana", "apple"]
In this tutorial, we learned How to Reverse an Array in Rust language with well detailed examples.