From b6a96e1bfe1a2e411a14fff13948236054a8cca5 Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Thu, 9 Jul 2020 17:51:00 +0200 Subject: [rust] High Score --- high-scores/src/lib.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 high-scores/src/lib.rs (limited to 'high-scores/src/lib.rs') diff --git a/high-scores/src/lib.rs b/high-scores/src/lib.rs new file mode 100644 index 0000000..a7e6c3d --- /dev/null +++ b/high-scores/src/lib.rs @@ -0,0 +1,29 @@ +#[derive(Debug)] +pub struct HighScores<'a> { + scores: &'a [u32] +} + +impl<'a> HighScores<'a> { + pub fn new(scores: &'a [u32]) -> Self { + HighScores{ scores } + } + + pub fn scores(&self) -> &[u32] { + self.scores + } + + pub fn latest(&self) -> Option { + self.scores.last().copied() + } + + pub fn personal_best(&self) -> Option { + self.scores.iter().max().copied() + } + + pub fn personal_top_three(&self) -> Vec { + let mut top3 = self.scores.to_vec(); + top3.sort_unstable_by(|a,b| b.cmp(a)); + top3.truncate(3); + top3 + } +} -- cgit v1.2.3