diff --git a/exercises/complex-type/src/enums.rs b/exercises/complex-type/src/enums.rs index d80b8585..b2dd823c 100644 --- a/exercises/complex-type/src/enums.rs +++ b/exercises/complex-type/src/enums.rs @@ -1,6 +1,7 @@ // Exercise 1 // Fill in the blank and fix the errors // Make it compile +#[derive(Debug)] enum MessageOne { Quit, Move { x: i32, y: i32 }, @@ -8,11 +9,11 @@ enum MessageOne { ChangeColor(i32, i32, i32), } fn show_message(msg: MessageOne) { - println!("{}", msg); + println!("{:?}", msg); } fn exercise1() { - let msgs: __ = [ + let msgs = [ MessageOne::Quit, MessageOne::Move { x: 1, y: 3 }, MessageOne::ChangeColor(255, 255, 0), @@ -29,6 +30,10 @@ fn exercise1() { // Run tests enum Message { // TODO: implement the message variant types based on their usage below + ChangeColor(u8, u8, u8), + Echo(String), + Move(Point), + Quit } struct Point { @@ -60,6 +65,12 @@ impl State { } fn process(&mut self, message: Message) { + match message { + Message::ChangeColor(a,b ,c ) => self.change_color((a, b, c)), + Message::Echo(s) => self.echo(s), + Message::Move(p) => self.move_position(p), + Message::Quit => self.quit(), + } // TODO: create a match expression to process the different message variants // Remember: When passing a tuple as a function argument, you'll need extra parentheses: fn function((t, u, p, l, e)) } @@ -69,6 +80,7 @@ impl State { // Exercise 3 // Fix the errors // Run tests +#[derive(Debug, PartialEq)] enum Direction { North, East, @@ -79,7 +91,10 @@ enum Direction { impl Direction { fn opposite(&self) -> Direction { match self { - //TODO + Direction::North => Direction::South, + Direction::East => Direction::West, + Direction::West => Direction::East, + Direction::South => Direction::North, } } } @@ -99,7 +114,10 @@ enum Operation { // Perform arithmetic operations fn perform_operation(operation: Operation, num1: f64, num2: f64) -> f64 { match operation { - // TODO + Operation::Add => num1 + num2, + Operation::Subtract => num1 - num2, + Operation::Divide => num1 / num2, + Operation::Multiply => num1 * num2, } } diff --git a/exercises/complex-type/src/structs.rs b/exercises/complex-type/src/structs.rs index d4ce0508..6e5cb93b 100644 --- a/exercises/complex-type/src/structs.rs +++ b/exercises/complex-type/src/structs.rs @@ -2,11 +2,21 @@ // Fix the error // Make it compile // Run test +#[derive(Debug)] struct Person { name: String, age: u8, hobby: String } +impl PartialEq for Person +{ + fn eq(&self, other: &Self) -> bool { + if self.name == other.name && self.age == other.age && self.hobby == other.hobby { + return true; + } + false + } +} fn exercise1() -> Person { let age = 30; // Hobby = Rust @@ -33,18 +43,18 @@ struct Agent { // Implementation of methods for the Person struct impl Agent { // Create a new Person instance - fn new(name: String, age: u32) -> Agent { - Agent { name, age } + fn new(name: String, age: u32) -> Self { + Self { name, age } } // Get the name of the person - fn get_name(&self) -> &str { - todo!() + fn get_name(&self) -> String { + self.name.to_string() } // Get the age of the person fn get_age(&self) -> u32 { - todo!() + self.age } } @@ -58,28 +68,28 @@ struct Calculator { impl Calculator { fn new() -> Self { - Calculator { value: 0 } + Self { value: 0 } } - fn add(&self, num: i32) { + fn add(&mut self, num: i32) { self.value += num; } - fn subtract(mut self, num: i32) { + fn subtract(&mut self, num: i32) { self.value -= num; } - fn clear(self) { + fn clear(&mut self) { self.value = 0; } - fn get_value(self) -> i32 { + fn get_value(&self) -> i32 { self.value } } // Exercise 4 // Make it compile -#[derive(Debug)] +#[derive(Debug, Clone)] struct User { first: String, last: String, @@ -95,7 +105,7 @@ fn exercise4() { let u2 = User { first: String::from("Mary"), - ..u1 + ..u1.clone() }; @@ -122,10 +132,10 @@ fn exercise5() { }); - let moved = foos[0]; + let moved = &foos[0]; - let moved_field = foos[0].str_val; + let moved_field = &foos[0].str_val; } // Exercise 6 @@ -153,12 +163,12 @@ impl Package { } } - fn is_international(&self) -> ??? { - // Something goes here... + fn is_international(&self) -> bool { + !self.sender_country.eq_ignore_ascii_case(self.recipient_country.as_str()) } - fn get_fees(&self, cents_per_gram: i32) -> ??? { - // Something goes here... + fn get_fees(&self, cents_per_gram: i32) -> i32 { + self.weight_in_grams * cents_per_gram } } @@ -177,6 +187,9 @@ mod tests { hobby:String::from("Rust") }; assert_eq!(p, p_expectation); + // assert_eq!(p.name, p_expectation.name); + // assert_eq!(p.age, p_expectation.age); + // assert_eq!(p.hobby, p_expectation.hobby); }