Add code
This commit is contained in:
1
example_6/.gitignore
vendored
Normal file
1
example_6/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
||||
6
example_6/Cargo.toml
Normal file
6
example_6/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "example_6"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
46
example_6/src/main.rs
Normal file
46
example_6/src/main.rs
Normal 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();
|
||||
}
|
||||
Reference in New Issue
Block a user