aboutsummaryrefslogtreecommitdiff
path: root/rust/grade-school/src/lib.rs
blob: 266fb1fd58c769f4a34313be03b1c0a6c95a4c7d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
use std::collections::{BTreeMap, BTreeSet};

#[derive(Default)]
pub struct School(BTreeMap<u32, BTreeSet<String>>);

impl School {
    pub fn new() -> School {
        Self::default()
    }

    pub fn add(&mut self, grade: u32, student: &str) {
        self.0.entry(grade).or_default().insert(student.to_string());
    }

    pub fn grades(&self) -> Vec<u32> {
        self.0.keys().cloned().collect()
    }

    pub fn grade(&self, grade: u32) -> Option<Vec<String>> {
        self.0.get(&grade).map(|vs| vs.iter().cloned().collect())
    }
}