Created
September 2, 2024 02:48
-
-
Save BurntNail/be9927cb130830636c2a90b37c62b161 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn main() { | |
| let target = Film { | |
| horror: 5.0, | |
| thriller: 4.0, | |
| romance: 0.0, | |
| comedy: 0.0, | |
| }; | |
| let films = [ | |
| ( | |
| "Conjuring", | |
| Film { | |
| horror: 5.0, | |
| thriller: 5.0, | |
| romance: 0.0, | |
| comedy: 2.0, | |
| }, | |
| ), | |
| ( | |
| "Anyone but you", | |
| Film { | |
| horror: 1.0, | |
| thriller: 1.0, | |
| romance: 4.0, | |
| comedy: 3.0, | |
| }, | |
| ), | |
| ( | |
| "Star Wars", | |
| Film { | |
| horror: 2.0, | |
| thriller: 5.0, | |
| romance: 1.0, | |
| comedy: 3.0, | |
| }, | |
| ), | |
| ]; | |
| for (name, film) in films { | |
| let mut distance_to_target = film.squared_distance_to_point(&target); | |
| println!("{name} is {distance_to_target}"); | |
| } | |
| } | |
| #[derive(Debug, Copy, Clone)] | |
| pub struct Film { | |
| horror: f32, | |
| thriller: f32, | |
| romance: f32, | |
| comedy: f32, | |
| } | |
| impl Film { | |
| pub fn squared_distance_to_point(&self, other: &Film) -> f32 { | |
| (self.horror - other.horror).powi(2) | |
| + (self.thriller - other.thriller).powi(2) | |
| + (self.romance - other.romance).powi(2) | |
| + (self.comedy - other.comedy).powi(2) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment