aboutsummaryrefslogtreecommitdiff
path: root/rust/grade-school/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'rust/grade-school/src/lib.rs')
-rw-r--r--rust/grade-school/src/lib.rs22
1 files changed, 22 insertions, 0 deletions
diff --git a/rust/grade-school/src/lib.rs b/rust/grade-school/src/lib.rs
new file mode 100644
index 0000000..266fb1f
--- /dev/null
+++ b/rust/grade-school/src/lib.rs
@@ -0,0 +1,22 @@
1use std::collections::{BTreeMap, BTreeSet};
2
3#[derive(Default)]
4pub struct School(BTreeMap<u32, BTreeSet<String>>);
5
6impl School {
7 pub fn new() -> School {
8 Self::default()
9 }
10
11 pub fn add(&mut self, grade: u32, student: &str) {
12 self.0.entry(grade).or_default().insert(student.to_string());
13 }
14
15 pub fn grades(&self) -> Vec<u32> {
16 self.0.keys().cloned().collect()
17 }
18
19 pub fn grade(&self, grade: u32) -> Option<Vec<String>> {
20 self.0.get(&grade).map(|vs| vs.iter().cloned().collect())
21 }
22}