A compilation of concepts I want to remember...

Navigation
 » Home
 » About Me
 » Github

Select sort in Rust

09 Jan 2017 » rust, algorithms

A first pass at using rust, an implementation of select sort algorithm…

fn main() {
    let mut xs : [i32; 6] = [50,20,70,10,60,30];
    let arr_len = xs.len();

    for o in 1..arr_len {
        let mut sm_ind = o;
        for i in o..arr_len {
            if xs[i].lt(&xs[sm_ind]) {
                sm_ind = i;
            }
        }
        xs.swap(o, sm_ind);
    }
    for n in 1..arr_len {
        print!("{} ", xs[n]);
    }
    println!();
}

A few things learned in this excercise:

  1. Need to use mut ahead of variable declaration as default is for immutable declaration.

  2. Type check used at compile time, as a lot of errors were a result of mismatched types.

  3. Feels like I just translated an implementation done in C, thus would be interested in seeing what a veteran rustecean implementation would look like.