Bubble Sort in Rust

Published on 03 October 2020 (Updated: 03 October 2020)

Welcome to the Bubble Sort in Rust page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

use std::env::args;
use std::process::exit;
use std::str::FromStr;

fn usage() -> ! {
    println!("Usage: please provide a list of at least two integers to sort in the format \"1, 2, 3, 4, 5\"");

fn parse_int<T: FromStr>(s: &str) -> Result<T, <T as FromStr>::Err> {

fn parse_int_list<T: FromStr>(s: &str) -> Result<Vec<T>, <T as FromStr>::Err> {
        .collect::<Result<Vec<T>, <T as FromStr>::Err>>()

fn bubble_sort<T: PartialOrd>(arr: &mut Vec<T>) {
    let mut swap_occured = true;

    while swap_occured {
        swap_occured = false;

        for i in 1..arr.len() {
            if arr[i - 1] > arr[i] {
                arr.swap(i - 1, i);
                swap_occured = true;

fn main() {
    let mut args = args().skip(1);

    // Convert 1st command-line argument to list of integers
    let mut arr: Vec<i32> = args
        .and_then(|s| parse_int_list(&s).ok())
        .unwrap_or_else(|| usage());

    // Exit if list too small
    if arr.len() < 2 {

    // Perform bubble sort and show results
    bubble_sort(&mut arr);

Bubble Sort in Rust was written by:

If you see anything you'd like to change or update, please consider contributing.

Note: The solution shown above is the current solution in the Sample Programs repository as of May 08 2023 19:53:07. The solution was first committed on Oct 03 2020 19:46:19. As a result, documentation below may be outdated.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.