diff --git a/solutions/rust/high-scores/1/Cargo.toml b/solutions/rust/high-scores/1/Cargo.toml new file mode 100644 index 00000000..1e3a7727 --- /dev/null +++ b/solutions/rust/high-scores/1/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "high_scores" +version = "0.1.0" +edition = "2024" + +# Not all libraries from crates.io are available in Exercism's test runner. +# The full list of available libraries is here: +# https://github.com/exercism/rust-test-runner/blob/main/local-registry/Cargo.toml +[dependencies] diff --git a/solutions/rust/high-scores/1/src/lib.rs b/solutions/rust/high-scores/1/src/lib.rs new file mode 100644 index 00000000..dd7ce3ae --- /dev/null +++ b/solutions/rust/high-scores/1/src/lib.rs @@ -0,0 +1,31 @@ +#[derive(Debug)] +pub struct HighScores { + scores_vec: Vec, +} + +impl HighScores { + pub fn new(scores: &[u32]) -> Self { + HighScores { + scores_vec: scores.to_vec(), + } + } + + pub fn scores(&self) -> &[u32] { + &self.scores_vec + } + + pub fn latest(&self) -> Option { + self.scores_vec.last().copied() + } + + pub fn personal_best(&self) -> Option { + self.scores_vec.iter().max().copied() + } + + pub fn personal_top_three(&self) -> Vec { + let mut sorted_scores = self.scores_vec.to_vec(); + sorted_scores.sort(); + sorted_scores.reverse(); + sorted_scores.iter().take(3).copied().collect() + } +} \ No newline at end of file