Highscores loading/overwriting

We already saw how to perform the I/O operations in the previous chapter, so it'll be very quick to do:

const NB_HIGHSCORES: usize = 5;

fn update_vec(v: &mut Vec<u32>, value: u32) -> bool {
    if v.len() < NB_HIGHSCORES {
        v.push(value);        v.sort();
        true
    } else {
        for entry in v.iter_mut() {
            if value > *entry {
                *entry = value;
                return true;
            }
        }
        false
    }
}

fn print_game_information(tetris: &Tetris) {
    let mut new_highest_highscore = true;
    let mut new_highest_lines_sent = true;
    if let Some((mut highscores, mut lines_sent)) =      load_highscores_and_lines() {
        new_highest_highscore = update_vec(&mut highscores,           tetris.score);
        new_highest_lines_sent = update_vec(&mut lines_sent,           tetris.nb_lines);
        if new_highest_highscore || ...

Get Rust Programming By Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.