How to Sort a Vector in Rust with Examples

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];
        println!("Sorted numbers vector: {:?}", numbers);
    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"];
        println!("Sorted cities Vector: {:?}", cities);
    Sorted cities Vector: ["Chicago", "Mumbai", "Paris", "Sydney", "Tokyo"]

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail:

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap