To sort an array in ascending order in Rust, you can use the `sort` method from the standard library for both arrays of integers and arrays of strings.
In this example,
numbers
.sort
method to sort the vector of integers in-place.fn main() {
let mut numbers = vec![5, 2, 8, 1, 9];
numbers.sort();
println!("Sorted Array of Integers: {:?}", numbers);
}
Sorted Array of Integers: [1, 2, 5, 8, 9]
In this example,
words
.sort
method to sort the vector of strings in-place based on string comparison.fn main() {
let mut words = vec!["apple", "banana", "cherry", "date".to_string()];
words.sort();
println!("Sorted Array of Strings: {:?}", words);
}
Sorted Array of Strings: ["apple", "banana", "cherry", "date"]
In this tutorial, we learned How to Sort an Array in Ascending Order in Rust language with well detailed examples.