This commit is contained in:
2025-03-26 12:41:58 -04:00
parent 637b26a0e9
commit 416dd617e6
36 changed files with 732 additions and 0 deletions

1
example_6/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

6
example_6/Cargo.toml Normal file
View File

@@ -0,0 +1,6 @@
[package]
name = "example_6"
version = "0.1.0"
edition = "2021"
[dependencies]

46
example_6/src/main.rs Normal file
View File

@@ -0,0 +1,46 @@
use std::fmt::Display;
struct GameScore<T> {
name: String,
scores: Vec<T>,
}
struct Example {
name: String,
}
impl Example {
fn new(name: String) -> Self {
Self { name }
}
}
impl<T: Ord> GameScore<T> {
fn high_score(&self) -> Option<&T> {
self.scores.iter().max()
}
}
impl<T: Display> GameScore<T> {
fn reverse_print_scores(&mut self) {
while let Some(last) = self.scores.pop() {
println!("{}", last);
}
}
}
fn main() {
let mut taylor_score = GameScore {
name: "taylor".to_string(),
scores: vec![Example::new("test".to_string())],
};
let mut bad_score = GameScore {
name: "taylor".to_string(),
scores: vec![10, 200, 3],
};
println!("{:?}", taylor_score.high_score());
taylor_score.reverse_print_scores();
}