From df3743c844a58ecdfee629c47c6e4596b73f3001 Mon Sep 17 00:00:00 2001 From: Chris Girvin Date: Mon, 16 May 2022 16:56:15 -0400 Subject: [PATCH 1/5] move_semantics done --- exercises/move_semantics/move_semantics4.rs | 36 ++++++++++++++--- exercises/move_semantics/move_semantics5.rs | 14 ++++++- exercises/move_semantics/move_semantics6.rs | 43 ++++++++++++++++++--- 3 files changed, 80 insertions(+), 13 deletions(-) diff --git a/exercises/move_semantics/move_semantics4.rs b/exercises/move_semantics/move_semantics4.rs index 2a23c710..c511fc61 100644 --- a/exercises/move_semantics/move_semantics4.rs +++ b/exercises/move_semantics/move_semantics4.rs @@ -4,12 +4,35 @@ // freshly created vector from fill_vec to its caller. // Execute `rustlings hint move_semantics4` for hints! -// I AM NOT DONE +// // original code +// fn main() { +// let vec0 = Vec::new(); +// let mut vec1 = fill_vec(vec0); + +// println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); + +// vec1.push(88); + +// println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); +// } + +// // original code (cont'd) +// // `fill_vec()` no longer takes `vec: Vec` as argument +// fn fill_vec() -> Vec { +// let mut vec = vec; + +// vec.push(22); +// vec.push(44); +// vec.push(66); + +// vec +// } +// // end of original code + +// solution fn main() { - let vec0 = Vec::new(); - - let mut vec1 = fill_vec(vec0); + let mut vec1 = fill_vec(); println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); @@ -18,9 +41,11 @@ fn main() { println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); } +// solution (cont'd) // `fill_vec()` no longer takes `vec: Vec` as argument fn fill_vec() -> Vec { - let mut vec = vec; + // create mutable vec + let mut vec = Vec::new(); vec.push(22); vec.push(44); @@ -28,3 +53,4 @@ fn fill_vec() -> Vec { vec } +// end of solution diff --git a/exercises/move_semantics/move_semantics5.rs b/exercises/move_semantics/move_semantics5.rs index c4704f9e..956f7b6f 100644 --- a/exercises/move_semantics/move_semantics5.rs +++ b/exercises/move_semantics/move_semantics5.rs @@ -3,13 +3,23 @@ // adding, changing or removing any of them. // Execute `rustlings hint move_semantics5` for hints :) -// I AM NOT DONE +// // original code +// fn main() { +// let mut x = 100; +// let y = &mut x; +// let z = &mut x; +// *y += 100; +// *z += 1000; +// assert_eq!(x, 1200); +// } +// // end of orignal code +// solution fn main() { let mut x = 100; let y = &mut x; - let z = &mut x; *y += 100; + let z = &mut x; *z += 1000; assert_eq!(x, 1200); } diff --git a/exercises/move_semantics/move_semantics6.rs b/exercises/move_semantics/move_semantics6.rs index 457e7ae7..12ae9cad 100644 --- a/exercises/move_semantics/move_semantics6.rs +++ b/exercises/move_semantics/move_semantics6.rs @@ -2,24 +2,55 @@ // Make me compile! `rustlings hint move_semantics6` for hints // You can't change anything except adding or removing references -// I AM NOT DONE +// // original code +// fn main() { +// let data = "Rust is great!".to_string(); +// get_char(data); + +// string_uppercase(&data); +// } + +// // original code (cont'd) +// // Should not take ownership +// fn get_char(data: String) -> char { +// data.chars().last().unwrap() +// } + +// // original code (cont'd) +// // Should take ownership +// fn string_uppercase(mut data: &String) { +// data = &data.to_uppercase(); + +// println!("{}", data); +// } +// // end of original code + +// solution fn main() { let data = "Rust is great!".to_string(); - get_char(data); + // change passed data to ref so as to not take ownership of `data` + get_char(&data); - string_uppercase(&data); + // remove ref so string_uppercase takes ownership of `data` + string_uppercase(data); } +// solution (cont'd) // Should not take ownership -fn get_char(data: String) -> char { +// add ref to argument type +fn get_char(data: &String) -> char { data.chars().last().unwrap() } +// solution (cont'd) // Should take ownership -fn string_uppercase(mut data: &String) { - data = &data.to_uppercase(); +// remove ref from argument type +fn string_uppercase(mut data: String) { + // remove ref from data + data = data.to_uppercase(); println!("{}", data); } +// end of solution From 087226959cdf861ae4f6eb6eb68cfae9f7eb0fd4 Mon Sep 17 00:00:00 2001 From: Chris Girvin Date: Mon, 16 May 2022 17:47:59 -0400 Subject: [PATCH 2/5] update break --- exercises/primitive_types/primitive_types1.rs | 6 ++---- exercises/primitive_types/primitive_types2.rs | 10 ++++------ exercises/primitive_types/primitive_types3.rs | 4 +--- exercises/primitive_types/primitive_types4.rs | 16 ++++++++++++++-- 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/exercises/primitive_types/primitive_types1.rs b/exercises/primitive_types/primitive_types1.rs index 09121392..49b523fd 100644 --- a/exercises/primitive_types/primitive_types1.rs +++ b/exercises/primitive_types/primitive_types1.rs @@ -2,17 +2,15 @@ // Fill in the rest of the line that has code missing! // No hints, there's no tricks, just get used to typing these :) -// I AM NOT DONE - fn main() { // Booleans (`bool`) - let is_morning = true; + let is_morning: bool = true; if is_morning { println!("Good morning!"); } - let // Finish the rest of this line like the example! Or make it be false! + let is_evening: bool = true; // Finish the rest of this line like the example! Or make it be false! if is_evening { println!("Good evening!"); } diff --git a/exercises/primitive_types/primitive_types2.rs b/exercises/primitive_types/primitive_types2.rs index 6576a4d5..4a8459c6 100644 --- a/exercises/primitive_types/primitive_types2.rs +++ b/exercises/primitive_types/primitive_types2.rs @@ -2,12 +2,10 @@ // Fill in the rest of the line that has code missing! // No hints, there's no tricks, just get used to typing these :) -// I AM NOT DONE - fn main() { // Characters (`char`) - let my_first_initial = 'C'; + let my_first_initial: char = 'C'; if my_first_initial.is_alphabetic() { println!("Alphabetical!"); } else if my_first_initial.is_numeric() { @@ -16,9 +14,9 @@ fn main() { println!("Neither alphabetic nor numeric!"); } - let // Finish this line like the example! What's your favorite character? - // Try a letter, try a number, try a special character, try a character - // from a different language than your own, try an emoji! + let your_character: char = '1'; // Finish this line like the example! What's your favorite character? + // Try a letter, try a number, try a special character, try a character + // from a different language than your own, try an emoji! if your_character.is_alphabetic() { println!("Alphabetical!"); } else if your_character.is_numeric() { diff --git a/exercises/primitive_types/primitive_types3.rs b/exercises/primitive_types/primitive_types3.rs index aaa518be..dd667b33 100644 --- a/exercises/primitive_types/primitive_types3.rs +++ b/exercises/primitive_types/primitive_types3.rs @@ -2,10 +2,8 @@ // Create an array with at least 100 elements in it where the ??? is. // Execute `rustlings hint primitive_types3` for hints! -// I AM NOT DONE - fn main() { - let a = ??? + let a = [0; 100]; if a.len() >= 100 { println!("Wow, that's a big array!"); diff --git a/exercises/primitive_types/primitive_types4.rs b/exercises/primitive_types/primitive_types4.rs index 10b553e9..42092e36 100644 --- a/exercises/primitive_types/primitive_types4.rs +++ b/exercises/primitive_types/primitive_types4.rs @@ -2,13 +2,25 @@ // Get a slice out of Array a where the ??? is so that the test passes. // Execute `rustlings hint primitive_types4` for hints!! -// I AM NOT DONE +// // original code +// #[test] +// fn slice_out_of_array() { +// let a = [1, 2, 3, 4, 5]; +// let nice_slice = ??? + +// assert_eq!([2, 3, 4], nice_slice) +// } +// // end of original code + +// solution #[test] fn slice_out_of_array() { let a = [1, 2, 3, 4, 5]; - let nice_slice = ??? + // create slice from borrowed array `a` + let nice_slice = &a[1..4]; assert_eq!([2, 3, 4], nice_slice) } +// end of solution From 884a0b6a5162d1e58b445a225099b02ee75e7fdc Mon Sep 17 00:00:00 2001 From: Chris Girvin Date: Mon, 16 May 2022 19:22:02 -0400 Subject: [PATCH 3/5] primitive_types done --- exercises/primitive_types/primitive_types5.rs | 4 +--- exercises/primitive_types/primitive_types6.rs | 8 +++----- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/exercises/primitive_types/primitive_types5.rs b/exercises/primitive_types/primitive_types5.rs index 680d8d23..5028c0cd 100644 --- a/exercises/primitive_types/primitive_types5.rs +++ b/exercises/primitive_types/primitive_types5.rs @@ -2,11 +2,9 @@ // Destructure the `cat` tuple so that the println will work. // Execute `rustlings hint primitive_types5` for hints! -// I AM NOT DONE - fn main() { let cat = ("Furry McFurson", 3.5); - let /* your pattern here */ = cat; + let (name, age)/* your pattern here */ = cat; println!("{} is {} years old.", name, age); } diff --git a/exercises/primitive_types/primitive_types6.rs b/exercises/primitive_types/primitive_types6.rs index b8c9b82b..a0c455af 100644 --- a/exercises/primitive_types/primitive_types6.rs +++ b/exercises/primitive_types/primitive_types6.rs @@ -3,14 +3,12 @@ // You can put the expression for the second element where ??? is so that the test passes. // Execute `rustlings hint primitive_types6` for hints! -// I AM NOT DONE - #[test] fn indexing_tuple() { let numbers = (1, 2, 3); // Replace below ??? with the tuple indexing syntax. - let second = ???; + // let second = ???; + let second = numbers.1; - assert_eq!(2, second, - "This is not the 2nd number in the tuple!") + assert_eq!(2, second, "This is not the 2nd number in the tuple!") } From d961e4695b5e5f33bc17e1dfd6cb112377ea6793 Mon Sep 17 00:00:00 2001 From: Chris Girvin Date: Mon, 16 May 2022 20:26:18 -0400 Subject: [PATCH 4/5] structs and enums done --- exercises/enums/enums1.rs | 6 +++-- exercises/enums/enums2.rs | 6 +++-- exercises/enums/enums3.rs | 42 ++++++++++++++++++++++++++++++----- exercises/structs/structs1.rs | 15 ++++++++----- exercises/structs/structs2.rs | 9 +++++--- exercises/structs/structs3.rs | 9 ++++---- 6 files changed, 65 insertions(+), 22 deletions(-) diff --git a/exercises/enums/enums1.rs b/exercises/enums/enums1.rs index a2223d33..2baa8f2b 100644 --- a/exercises/enums/enums1.rs +++ b/exercises/enums/enums1.rs @@ -1,11 +1,13 @@ // enums1.rs // Make me compile! Execute `rustlings hint enums1` for hints! -// 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 ec32d952..287fcac4 100644 --- a/exercises/enums/enums2.rs +++ b/exercises/enums/enums2.rs @@ -1,11 +1,13 @@ // enums2.rs // Make me compile! Execute `rustlings hint enums2` for hints! -// I AM NOT DONE - #[derive(Debug)] enum Message { // TODO: define the different variants used below + Move { x: i32, y: i32 }, + Echo(String), + ChangeColor(u8, u8, u8), + Quit, } impl Message { diff --git a/exercises/enums/enums3.rs b/exercises/enums/enums3.rs index 178b40c4..48aaaff5 100644 --- a/exercises/enums/enums3.rs +++ b/exercises/enums/enums3.rs @@ -1,10 +1,18 @@ // enums3.rs // Address all the TODOs to make the tests pass! -// I AM NOT DONE - enum Message { // TODO: implement the message variant types based on their usage below + // // solution0 + // ChangeColor((u8, u8, u8)), + // Echo(String), + // Move(Point), + // Quit, + // solution1 + ChangeColor { color: (u8, u8, u8) }, + Echo { s: String }, + Move { p: Point }, + Quit, } struct Point { @@ -37,6 +45,18 @@ impl State { fn process(&mut self, message: Message) { // TODO: create a match expression to process the different message variants + match message { + // // solution0 + // Message::ChangeColor((r, g, b)) => self.change_color((r, g, b)), + // Message::Echo(s) => self.echo(s), + // Message::Move(p) => self.move_position(p), + // Message::Quit => self.quit(), + // solution1 + Message::ChangeColor { color } => self.change_color(color), + Message::Echo { s } => self.echo(s), + Message::Move { p } => self.move_position(p), + Message::Quit => self.quit(), + } } } @@ -51,9 +71,21 @@ mod tests { position: Point { x: 0, y: 0 }, color: (0, 0, 0), }; - state.process(Message::ChangeColor((255, 0, 255))); - state.process(Message::Echo(String::from("hello world"))); - state.process(Message::Move(Point { x: 10, y: 15 })); + // // solution0 + // state.process(Message::ChangeColor((255, 0, 255))); + // state.process(Message::Echo(String::from("hello world"))); + // state.process(Message::Move(Point { x: 10, y: 15 })); + // state.process(Message::Quit); + // solution1 + state.process(Message::ChangeColor { + color: (255, 0, 255), + }); + state.process(Message::Echo { + s: String::from("hello world"), + }); + state.process(Message::Move { + p: Point { x: 10, y: 15 }, + }); state.process(Message::Quit); assert_eq!(state.color, (255, 0, 255)); diff --git a/exercises/structs/structs1.rs b/exercises/structs/structs1.rs index 6d0b2f49..5fd2fb9c 100644 --- a/exercises/structs/structs1.rs +++ b/exercises/structs/structs1.rs @@ -1,13 +1,13 @@ // structs1.rs // Address all the TODOs to make the tests pass! -// I AM NOT DONE - struct ColorClassicStruct { // TODO: Something goes here + name: String, + hex: String, } -struct ColorTupleStruct(/* TODO: Something goes here */); +struct ColorTupleStruct(String, String /* TODO: Something goes here */); #[derive(Debug)] struct UnitStruct; @@ -19,7 +19,10 @@ mod tests { #[test] fn classic_c_structs() { // TODO: Instantiate a classic c struct! - // let green = + let green = ColorClassicStruct { + name: String::from("green"), + hex: String::from("#00FF00"), + }; assert_eq!(green.name, "green"); assert_eq!(green.hex, "#00FF00"); @@ -28,7 +31,7 @@ mod tests { #[test] fn tuple_structs() { // TODO: Instantiate a tuple struct! - // let green = + let green = ColorTupleStruct(String::from("green"), String::from("#00FF00")); assert_eq!(green.0, "green"); assert_eq!(green.1, "#00FF00"); @@ -37,7 +40,7 @@ mod tests { #[test] fn unit_structs() { // TODO: Instantiate a unit struct! - // let unit_struct = + let unit_struct = UnitStruct; let message = format!("{:?}s are fun!", unit_struct); assert_eq!(message, "UnitStructs are fun!"); diff --git a/exercises/structs/structs2.rs b/exercises/structs/structs2.rs index f9c6427d..8d31d82b 100644 --- a/exercises/structs/structs2.rs +++ b/exercises/structs/structs2.rs @@ -1,8 +1,6 @@ // structs2.rs // Address all the TODOs to make the tests pass! -// I AM NOT DONE - #[derive(Debug)] struct Order { name: String, @@ -34,7 +32,12 @@ 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 e84f2ebc..1a2bf008 100644 --- a/exercises/structs/structs3.rs +++ b/exercises/structs/structs3.rs @@ -4,8 +4,6 @@ // Make the code compile and the tests pass! // If you have issues execute `rustlings hint structs3` -// I AM NOT DONE - #[derive(Debug)] struct Package { sender_country: String, @@ -17,6 +15,7 @@ impl Package { fn new(sender_country: String, recipient_country: String, weight_in_grams: i32) -> Package { if weight_in_grams <= 0 { // panic statement goes here... + panic!("Panic!") } else { Package { sender_country, @@ -26,12 +25,14 @@ impl Package { } } - fn is_international(&self) -> ??? { + fn is_international(&self) -> bool { // Something goes here... + self.sender_country.ne(&self.recipient_country) } - fn get_fees(&self, cents_per_gram: i32) -> ??? { + fn get_fees(&self, cents_per_gram: i32) -> i32 { // Something goes here... + cents_per_gram * self.weight_in_grams } } From 95a9294c018036ed178ee36d294796b67d00cabf Mon Sep 17 00:00:00 2001 From: Chris Girvin Date: Mon, 16 May 2022 20:39:55 -0400 Subject: [PATCH 5/5] modules done --- exercises/modules/modules1.rs | 4 +--- exercises/modules/modules2.rs | 6 ++---- exercises/modules/modules3.rs | 9 ++++++--- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/exercises/modules/modules1.rs b/exercises/modules/modules1.rs index 1a2bd0dd..3535a36a 100644 --- a/exercises/modules/modules1.rs +++ b/exercises/modules/modules1.rs @@ -1,15 +1,13 @@ // modules1.rs // Make me compile! Execute `rustlings hint modules1` for hints :) -// I AM NOT DONE - mod sausage_factory { // Don't let anybody outside of this module see this! fn get_secret_recipe() -> String { 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 87f0c458..8d25567b 100644 --- a/exercises/modules/modules2.rs +++ b/exercises/modules/modules2.rs @@ -3,13 +3,11 @@ // 'use' and 'as' keywords. Fix these 'use' statements to make the code compile. // Make me compile! Execute `rustlings hint modules2` for hints :) -// 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 8eed77df..60e51ce9 100644 --- a/exercises/modules/modules3.rs +++ b/exercises/modules/modules3.rs @@ -5,10 +5,13 @@ // from the std::time module. Bonus style points if you can do it with one line! // Make me compile! Execute `rustlings hint modules3` for hints :) -// I AM NOT DONE - // TODO: Complete this use statement -use ??? +// solution0 +// use std::time::SystemTime; +// use std::time::UNIX_EPOCH; +// solution1 +// use unix shell-style glob pattern matching +use std::time::{SystemTime, UNIX_EPOCH}; fn main() { match SystemTime::now().duration_since(UNIX_EPOCH) {