CS3984 Computer Systems in Rust



Live Demo - Basics

enum Curve {
    Texas,
    Flat(f32),
}

struct Class {
    score: f32,
    curve: Curve,
}

impl Class {
    fn new(score: f32, curve: f32) -> Class {
        Class { score, curve: Curve::Flat(curve) }
    }

    fn without_curve(score: f32) -> Class {
        Class { score, curve: Curve::Flat(0.0) }
    }

    fn texas_curve(score: f32) -> Class {
        Class { score, curve: Curve::Texas }
    }

    fn effective_score(&self) -> f32 {
        match self.curve {
            Curve::Texas => self.score.sqrt() * 10.0,
            Curve::Flat(curve) => self.score + curve,
        }
    }
}

fn calculate_average(classes: &[Class; 3]) -> f32 {
    let mut total_score = 0.0;
    for class in classes {
        total_score += class.effective_score();
    }
    total_score / classes.len() as f32
}

fn main() {
    let scores: [Class; 3] = [
        Class::without_curve(100.0),
        Class::texas_curve(75.0),
        Class::new(80.0, 5.0),
    ];
    let average = calculate_average(&scores);
    println!("Average score: {average:?}");
}