mirror of
https://github.com/ArthurDanjou/exercism-rust.git
synced 2026-01-29 19:30:28 +01:00
Initial commit
This commit is contained in:
31
high-scores/src/lib.rs
Normal file
31
high-scores/src/lib.rs
Normal file
@@ -0,0 +1,31 @@
|
||||
#[derive(Debug)]
|
||||
pub struct HighScores<'a> {
|
||||
scores: &'a [u32]
|
||||
}
|
||||
|
||||
impl<'a> HighScores<'a> {
|
||||
pub fn new(scores: &'a [u32]) -> Self {
|
||||
Self {
|
||||
scores
|
||||
}
|
||||
}
|
||||
|
||||
pub fn scores(&self) -> &[u32] {
|
||||
self.scores
|
||||
}
|
||||
|
||||
pub fn latest(&self) -> Option<u32> {
|
||||
self.scores.last().cloned()
|
||||
}
|
||||
|
||||
pub fn personal_best(&self) -> Option<u32> {
|
||||
self.scores.iter().max().cloned()
|
||||
}
|
||||
|
||||
pub fn personal_top_three(&self) -> Vec<u32> {
|
||||
let mut top_vec = self.scores.to_vec();
|
||||
top_vec.sort_unstable_by(|a, b| b.cmp(a));
|
||||
top_vec.truncate(3);
|
||||
top_vec
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user