We can make use of the sort() method to sort a Vector in Rust Language.
Let's take a look at a few examples:
Example 1: Sort a vector of Numbers
fn main() {
let mut numbers = vec![15, 22, 37, 13, 91, 32, 42];
numbers.sort();
println!("Sorted numbers vector: {:?}", numbers);
}
Output:
Sorted numbers vector: [13, 15, 22, 32, 37, 42, 91]
Example 2: Sort a vector of Strings
fn main() {
let mut cities = vec!["Chicago", "Mumbai", "Paris", "Tokyo", "Sydney"];
cities.sort();
println!("Sorted cities Vector: {:?}", cities);
}
Output:
Sorted cities Vector: ["Chicago", "Mumbai", "Paris", "Sydney", "Tokyo"]
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Rust,
- Rust: Cargo Init vs Cargo New Command
- Rust: Write and Run Hello World! Program Example
- How to Split a String using Rust Language
- How to Sort a Vector in Rust with Examples
- Fix: error: could not find `Cargo.toml` in Users or any parent directory
- How to uninstall Rust Language from Mac/Linux/Ubuntu
- How to update Cargo (Rust Lang)
- Fix: rust-analyzer failed to discover workspace [Visual Studio Code]
- How to install Rust using rustup on macOS/Linux/Ubuntu
- Cargo Watch: To Recompile Rust Project Automatically
- Difference between rustc and cargo build commands
- How to Split a String by Space in Rust
- How to know Rust is Installed on Mac?
- Rust: zsh: no such file or directory: ./main
- How to update Rust on Mac/Linux
- List of Rust Cargo Commands
- How to find version of Cargo in Rust
- Fix: error: mismatched closing delimiter } [Rust]
More Posts:
- How to determine Gradle Version in Android Studio - Android-Studio
- CentOS Cannot find a valid baseurl for repo base7x86_64 yum - HowTos
- 29: Program to move a file in Python - Python
- [Error] Microsoft Teams: We're sorryβwe've run into an issue. - Microsoft
- How to run a Command in Bash Script - Bash
- How to perform Find and Replace using vim Editor - vi
- Shutdown Mac Terminal Command - MacOS
- How to check if variable is a number in JavaScript (NaN, typeof, regex) - JavaScript