mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-05-15 17:58:44 +00:00
Merge pull request #6 from Weltenbummler397/exercism-sync/f73acc3564f101aa
[Sync Iteration] rust/high-scores/1
This commit is contained in:
commit
687d15216a
9
solutions/rust/high-scores/1/Cargo.toml
Normal file
9
solutions/rust/high-scores/1/Cargo.toml
Normal file
@ -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]
|
||||
31
solutions/rust/high-scores/1/src/lib.rs
Normal file
31
solutions/rust/high-scores/1/src/lib.rs
Normal file
@ -0,0 +1,31 @@
|
||||
#[derive(Debug)]
|
||||
pub struct HighScores {
|
||||
scores_vec: Vec<u32>,
|
||||
}
|
||||
|
||||
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<u32> {
|
||||
self.scores_vec.last().copied()
|
||||
}
|
||||
|
||||
pub fn personal_best(&self) -> Option<u32> {
|
||||
self.scores_vec.iter().max().copied()
|
||||
}
|
||||
|
||||
pub fn personal_top_three(&self) -> Vec<u32> {
|
||||
let mut sorted_scores = self.scores_vec.to_vec();
|
||||
sorted_scores.sort();
|
||||
sorted_scores.reverse();
|
||||
sorted_scores.iter().take(3).copied().collect()
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user