From 26f785a0ffcef9c8fcff7cf6f8374735998b3528 Mon Sep 17 00:00:00 2001 From: Reality Enjoyer Date: Fri, 16 Jan 2026 08:20:49 +0000 Subject: [PATCH] structs and enums --- exercises/07_structs/structs3.rs | 17 ++++------------- exercises/08_enums/enums1.rs | 6 +++++- exercises/08_enums/enums2.rs | 6 +++++- exercises/08_enums/enums3.rs | 9 +++++++-- exercises/09_strings/strings1.rs | 2 +- exercises/09_strings/strings2.rs | 2 +- 6 files changed, 23 insertions(+), 19 deletions(-) diff --git a/exercises/07_structs/structs3.rs b/exercises/07_structs/structs3.rs index 69e5ced..03808b9 100644 --- a/exercises/07_structs/structs3.rs +++ b/exercises/07_structs/structs3.rs @@ -1,6 +1,3 @@ -// Structs contain data, but can also have logic. In this exercise, we have -// defined the `Package` struct, and we want to test some logic attached to it. - #[derive(Debug)] struct Package { sender_country: String, @@ -11,8 +8,6 @@ struct Package { impl Package { fn new(sender_country: String, recipient_country: String, weight_in_grams: u32) -> Self { if weight_in_grams < 10 { - // This isn't how you should handle errors in Rust, but we will - // learn about error handling later. panic!("Can't ship a package with weight below 10 grams"); } @@ -23,15 +18,12 @@ impl Package { } } - // TODO: Add the correct return type to the function signature. - fn is_international(&self) { - // TODO: Read the tests that use this method to find out when a package - // is considered international. + fn is_international(&self) -> bool { + self.sender_country != self.recipient_country } - // TODO: Add the correct return type to the function signature. - fn get_fees(&self, cents_per_gram: u32) { - // TODO: Calculate the package's fees. + fn get_fees(&self, cents_per_gram: u32) -> u32 { + cents_per_gram * self.weight_in_grams } } @@ -68,7 +60,6 @@ mod tests { let recipient_country = sender_country.clone(); let package = Package::new(sender_country, recipient_country, 1200); - assert!(!package.is_international()); } diff --git a/exercises/08_enums/enums1.rs b/exercises/08_enums/enums1.rs index c0d0c30..97a5cc0 100644 --- a/exercises/08_enums/enums1.rs +++ b/exercises/08_enums/enums1.rs @@ -1,6 +1,10 @@ #[derive(Debug)] enum Message { - // TODO: Define a few types of messages as used below. + Resize, + Move, + Echo, + ChangeColor, + Quit, } fn main() { diff --git a/exercises/08_enums/enums2.rs b/exercises/08_enums/enums2.rs index d70f639..07aee26 100644 --- a/exercises/08_enums/enums2.rs +++ b/exercises/08_enums/enums2.rs @@ -6,7 +6,11 @@ struct Point { #[derive(Debug)] enum Message { - // TODO: Define the different variants used below. + Resize { width: u64, height: u64 }, + Move(Point), + Echo(String), + ChangeColor(u8, u8, u8), + Quit, } impl Message { diff --git a/exercises/08_enums/enums3.rs b/exercises/08_enums/enums3.rs index cb05f65..c60c17d 100644 --- a/exercises/08_enums/enums3.rs +++ b/exercises/08_enums/enums3.rs @@ -44,8 +44,13 @@ impl State { } fn process(&mut self, message: Message) { - // TODO: Create a match expression to process the different message - // variants using the methods defined above. + match message { + Message::Resize { width, height } => self.resize(width, height), + Message::Move(point) => self.move_position(point), + Message::Echo(string) => self.echo(string), + Message::Quit => self.quit(), + Message::ChangeColor(r, g, b) => self.change_color(r, g, b), + } } } diff --git a/exercises/09_strings/strings1.rs b/exercises/09_strings/strings1.rs index 6abdbb4..06755aa 100644 --- a/exercises/09_strings/strings1.rs +++ b/exercises/09_strings/strings1.rs @@ -1,6 +1,6 @@ // TODO: Fix the compiler error without changing the function signature. fn current_favorite_color() -> String { - "blue" + "blue".to_string() } fn main() { diff --git a/exercises/09_strings/strings2.rs b/exercises/09_strings/strings2.rs index 93d9cb6..2ed89c5 100644 --- a/exercises/09_strings/strings2.rs +++ b/exercises/09_strings/strings2.rs @@ -6,7 +6,7 @@ fn is_a_color_word(attempt: &str) -> bool { fn main() { let word = String::from("green"); // Don't change this line. - if is_a_color_word(word) { + if is_a_color_word(&word) { println!("That is a color word I know!"); } else { println!("That is not a color word I know.");