diff --git a/exercises/enums/enums1.rs b/exercises/enums/enums1.rs index 511ba740..535d21e1 100644 --- a/exercises/enums/enums1.rs +++ b/exercises/enums/enums1.rs @@ -1,11 +1,13 @@ // enums1.rs // No hints this time! ;) -// I AM NOT DONE #[derive(Debug)] enum Message { - // TODO: define a few types of messages as used below + Quit, + Echo, + Move, + ChangeColor } fn main() { diff --git a/exercises/enums/enums2.rs b/exercises/enums/enums2.rs index 18479f87..cdfa9072 100644 --- a/exercises/enums/enums2.rs +++ b/exercises/enums/enums2.rs @@ -1,11 +1,12 @@ // enums2.rs // Execute `rustlings hint enums2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - #[derive(Debug)] enum Message { - // TODO: define the different variants used below + Quit, + Echo(String), + Move{x:i32,y:i32}, + ChangeColor(i32,i32,i32) } impl Message { diff --git a/exercises/enums/enums3.rs b/exercises/enums/enums3.rs index 55acf6bc..f079cb2a 100644 --- a/exercises/enums/enums3.rs +++ b/exercises/enums/enums3.rs @@ -2,10 +2,11 @@ // Address all the TODOs to make the tests pass! // Execute `rustlings hint enums3` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - enum Message { - // TODO: implement the message variant types based on their usage below + Quit, + Echo(String), + Move(Point), + ChangeColor((u8,u8,u8)) } struct Point { @@ -38,6 +39,12 @@ impl State { fn process(&mut self, message: Message) { // TODO: create a match expression to process the different message variants + match message { + Message::Quit => self.quit(), + Message::Echo(s) => self.echo(s), + Message::Move(m) => self.move_position(m), + Message::ChangeColor((r,g,b)) => self.change_color((r,g,b)) + } } } diff --git a/exercises/modules/modules1.rs b/exercises/modules/modules1.rs index 8dd0e402..593b1171 100644 --- a/exercises/modules/modules1.rs +++ b/exercises/modules/modules1.rs @@ -1,7 +1,6 @@ // modules1.rs // Execute `rustlings hint modules1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE mod sausage_factory { // Don't let anybody outside of this module see this! @@ -9,7 +8,7 @@ mod sausage_factory { String::from("Ginger") } - fn make_sausage() { + pub fn make_sausage() { get_secret_recipe(); println!("sausage!"); } diff --git a/exercises/modules/modules2.rs b/exercises/modules/modules2.rs index c30a3897..65ea5a83 100644 --- a/exercises/modules/modules2.rs +++ b/exercises/modules/modules2.rs @@ -3,12 +3,10 @@ // 'use' and 'as' keywords. Fix these 'use' statements to make the code compile. // Execute `rustlings hint modules2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - mod delicious_snacks { // TODO: Fix these use statements - use self::fruits::PEAR as ??? - use self::veggies::CUCUMBER as ??? + pub use self::fruits::PEAR as fruit; + pub use self::veggies::CUCUMBER as veggie; mod fruits { pub const PEAR: &'static str = "Pear"; diff --git a/exercises/modules/modules3.rs b/exercises/modules/modules3.rs index 35e07990..181d5a35 100644 --- a/exercises/modules/modules3.rs +++ b/exercises/modules/modules3.rs @@ -5,10 +5,9 @@ // from the std::time module. Bonus style points if you can do it with one line! // Execute `rustlings hint modules3` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE // TODO: Complete this use statement -use ??? +use std::time::{SystemTime, UNIX_EPOCH}; fn main() { match SystemTime::now().duration_since(UNIX_EPOCH) { diff --git a/exercises/strings/strings1.rs b/exercises/strings/strings1.rs index 0de86a1d..fd74ea38 100644 --- a/exercises/strings/strings1.rs +++ b/exercises/strings/strings1.rs @@ -2,13 +2,11 @@ // Make me compile without changing the function signature! // Execute `rustlings hint strings1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - fn main() { let answer = current_favorite_color(); println!("My current favorite color is {}", answer); } fn current_favorite_color() -> String { - "blue" + "blue".to_string() } diff --git a/exercises/strings/strings2.rs b/exercises/strings/strings2.rs index 0c48ec95..87a20c78 100644 --- a/exercises/strings/strings2.rs +++ b/exercises/strings/strings2.rs @@ -2,11 +2,9 @@ // Make me compile without changing the function signature! // Execute `rustlings hint strings2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - fn main() { let word = String::from("green"); // Try not changing this line :) - if is_a_color_word(word) { + if is_a_color_word(word.as_str()) { println!("That is a color word I know!"); } else { println!("That is not a color word I know."); diff --git a/exercises/strings/strings3.rs b/exercises/strings/strings3.rs index e2353aec..bdad5a30 100644 --- a/exercises/strings/strings3.rs +++ b/exercises/strings/strings3.rs @@ -1,21 +1,17 @@ // strings3.rs // Execute `rustlings hint strings3` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - fn trim_me(input: &str) -> String { - // TODO: Remove whitespace from both ends of a string! - ??? + return String::from(input.trim()); } fn compose_me(input: &str) -> String { - // TODO: Add " world!" to the string! There's multiple ways to do this! - ??? + return String::from(input.to_owned() + &String::from(" world!")); } fn replace_me(input: &str) -> String { // TODO: Replace "cars" in the string with "balloons"! - ??? + String::from(input.replace("cars","balloons")) } #[cfg(test)] diff --git a/exercises/strings/strings4.rs b/exercises/strings/strings4.rs index c410b562..c5caaf8b 100644 --- a/exercises/strings/strings4.rs +++ b/exercises/strings/strings4.rs @@ -6,8 +6,6 @@ // before the parentheses on each line. If you're right, it will compile! // No hints this time! -// I AM NOT DONE - fn string_slice(arg: &str) { println!("{}", arg); } @@ -16,14 +14,14 @@ fn string(arg: String) { } fn main() { - ???("blue"); - ???("red".to_string()); - ???(String::from("hi")); - ???("rust is fun!".to_owned()); - ???("nice weather".into()); - ???(format!("Interpolation {}", "Station")); - ???(&String::from("abc")[0..1]); - ???(" hello there ".trim()); - ???("Happy Monday!".to_string().replace("Mon", "Tues")); - ???("mY sHiFt KeY iS sTiCkY".to_lowercase()); + string_slice("blue"); + string("red".to_string()); + string(String::from("hi")); + string("rust is fun!".to_owned()); + string_slice("nice weather".into()); + string(format!("Interpolation {}", "Station")); + string_slice(&String::from("abc")[0..1]); + string_slice(" hello there ".trim()); + string("Happy Monday!".to_string().replace("Mon", "Tues")); + string("mY sHiFt KeY iS sTiCkY".to_lowercase()); } diff --git a/exercises/structs/structs1.rs b/exercises/structs/structs1.rs index 0d91c469..ebc618d5 100644 --- a/exercises/structs/structs1.rs +++ b/exercises/structs/structs1.rs @@ -2,16 +2,19 @@ // Address all the TODOs to make the tests pass! // Execute `rustlings hint structs1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE struct ColorClassicStruct { - // TODO: Something goes here + red: i32, + green: i32, + blue: i32, } -struct ColorTupleStruct(/* TODO: Something goes here */); +struct ColorTupleStruct(i32,i32,i32); #[derive(Debug)] -struct UnitLikeStruct; +struct UnitLikeStruct{} + + #[cfg(test)] mod tests { @@ -19,8 +22,12 @@ mod tests { #[test] fn classic_c_structs() { - // TODO: Instantiate a classic c struct! - // let green = + // DONE: Instantiate a classic c struct! + let green = ColorClassicStruct{ + red: 0, + green: 255, + blue: 0 + }; assert_eq!(green.red, 0); assert_eq!(green.green, 255); @@ -29,8 +36,8 @@ mod tests { #[test] fn tuple_structs() { - // TODO: Instantiate a tuple struct! - // let green = + // DONE: Instantiate a tuple struct! + let green = ColorTupleStruct(0,255,0); assert_eq!(green.0, 0); assert_eq!(green.1, 255); @@ -39,8 +46,8 @@ mod tests { #[test] fn unit_structs() { - // TODO: Instantiate a unit-like struct! - // let unit_like_struct = + // DONE: Instantiate a unit-like struct! + let unit_like_struct = UnitLikeStruct{}; let message = format!("{:?}s are fun!", unit_like_struct); assert_eq!(message, "UnitLikeStructs are fun!"); diff --git a/exercises/structs/structs2.rs b/exercises/structs/structs2.rs index 32e311fa..046208f8 100644 --- a/exercises/structs/structs2.rs +++ b/exercises/structs/structs2.rs @@ -2,9 +2,8 @@ // Address all the TODOs to make the tests pass! // Execute `rustlings hint structs2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE -#[derive(Debug)] +#[derive(Clone,Debug)] struct Order { name: String, year: u32, @@ -35,7 +34,11 @@ mod tests { fn your_order() { let order_template = create_order_template(); // TODO: Create your own order using the update syntax and template above! - // let your_order = + let your_order = Order { + name: String::from("Hacker in Rust"), + count: 1, + ..order_template + }; assert_eq!(your_order.name, "Hacker in Rust"); assert_eq!(your_order.year, order_template.year); assert_eq!(your_order.made_by_phone, order_template.made_by_phone); diff --git a/exercises/structs/structs3.rs b/exercises/structs/structs3.rs index 0b3615f4..c29e1ab3 100644 --- a/exercises/structs/structs3.rs +++ b/exercises/structs/structs3.rs @@ -4,7 +4,6 @@ // Make the code compile and the tests pass! // Execute `rustlings hint structs3` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE #[derive(Debug)] struct Package { @@ -26,12 +25,17 @@ impl Package { } } - fn is_international(&self) -> ??? { - // Something goes here... + fn is_international(&self) -> bool { + let mut is_international = false; + if &self.sender_country != &self.recipient_country{ + is_international = true; + } + is_international + } - fn get_fees(&self, cents_per_gram: i32) -> ??? { - // Something goes here... + fn get_fees(&self, cents_per_gram: i32) -> i32 { + cents_per_gram * &self.weight_in_grams } }