Introduction to Rust

Recording from the parallel systems M2R course. Initial introduction to rust.

Due to the current covid situation I will put recordings of my courses online. Please note that these are raw un-edited recordings of the course with no special microphone. I also speak through my mask. Videos are streamed with webtorrent.

So for today, two videos. The first one is my commentary on the slides and the second one is about coding a min-max example.

slides commentary
min max

In case it does not stream, here is a direct link to the slide commentary and another one on the example.

You can also take a direct look at the final min-max code:

#[derive(Debug)]
enum MinMax<T> {
    Single(T),
    Double(T, T),
}

fn min_max2<I: Iterator<Item = T>, T: Ord>(mut elements: I) -> MinMax<T> {
    let current = MinMax::Single(elements.next().unwrap());
    elements.fold(current, |state, x| match state {
        MinMax::Single(mm) => {
            if x < mm {
                MinMax::Double(x, mm)
            } else {
                if x > mm {
                    MinMax::Double(mm, x)
                } else {
                    MinMax::Single(mm)
                }
            }
        }
        MinMax::Double(min, max) => {
            if x < min {
                MinMax::Double(x, max)
            } else {
                if x > max {
                    MinMax::Double(min, x)
                } else {
                    MinMax::Double(min, max)
                }
            }
        }
    })
}

fn min_max<I: Iterator<Item = T>, T: Ord + Copy>(mut elements: I) -> (T, T) {
    let current_min = elements.next().unwrap();
    let current_max = current_min;
    elements.fold((current_min, current_max), |(old_min, old_max), x| {
        (old_min.min(x), old_max.max(x))
    })
}

fn main() {
    let mut v: Vec<_> = std::iter::repeat_with(rand::random::<u32>)
        .map(|e| e % 10)
        .take(10)
        .collect();
    println!("v is : {:?}", v);

    let r = min_max2(v.iter_mut());

    println!("r: {:?}", r);
}