diff options
| author | Federico Igne <git@federicoigne.com> | 2020-07-09 17:51:00 +0200 |
|---|---|---|
| committer | Federico Igne <git@federicoigne.com> | 2021-11-03 18:55:08 +0000 |
| commit | b6a96e1bfe1a2e411a14fff13948236054a8cca5 (patch) | |
| tree | 38079dbf70091a049b586b504c572bed43d78c33 /high-scores/src | |
| parent | 1bbdf8a1d5490737388d435831639938cec439e0 (diff) | |
| download | exercism-b6a96e1bfe1a2e411a14fff13948236054a8cca5.tar.gz exercism-b6a96e1bfe1a2e411a14fff13948236054a8cca5.zip | |
[rust] High Score
Diffstat (limited to 'high-scores/src')
| -rw-r--r-- | high-scores/src/lib.rs | 29 |
1 files changed, 29 insertions, 0 deletions
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 @@ | |||
| 1 | #[derive(Debug)] | ||
| 2 | pub struct HighScores<'a> { | ||
| 3 | scores: &'a [u32] | ||
| 4 | } | ||
| 5 | |||
| 6 | impl<'a> HighScores<'a> { | ||
| 7 | pub fn new(scores: &'a [u32]) -> Self { | ||
| 8 | HighScores{ scores } | ||
| 9 | } | ||
| 10 | |||
| 11 | pub fn scores(&self) -> &[u32] { | ||
| 12 | self.scores | ||
| 13 | } | ||
| 14 | |||
| 15 | pub fn latest(&self) -> Option<u32> { | ||
| 16 | self.scores.last().copied() | ||
| 17 | } | ||
| 18 | |||
| 19 | pub fn personal_best(&self) -> Option<u32> { | ||
| 20 | self.scores.iter().max().copied() | ||
| 21 | } | ||
| 22 | |||
| 23 | pub fn personal_top_three(&self) -> Vec<u32> { | ||
| 24 | let mut top3 = self.scores.to_vec(); | ||
| 25 | top3.sort_unstable_by(|a,b| b.cmp(a)); | ||
| 26 | top3.truncate(3); | ||
| 27 | top3 | ||
| 28 | } | ||
| 29 | } | ||
