From 301e378b4d208e7a913219366fbcb43080f4cf7d Mon Sep 17 00:00:00 2001 From: Eri Mendz <erimendz@gmail.com> Date: Mon, 31 May 2021 11:09:25 +0300 Subject: [PATCH] Add solution to structs2.rs --- exercises/structs/structs2.rs | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/exercises/structs/structs2.rs b/exercises/structs/structs2.rs index b4a3874..21f4316 100644 --- a/exercises/structs/structs2.rs +++ b/exercises/structs/structs2.rs @@ -1,7 +1,7 @@ // structs2.rs // Address all the TODOs to make the tests pass! -// I AM NOT DONE +// Not Done #[derive(Debug)] struct Order { @@ -26,6 +26,12 @@ fn create_order_template() -> Order { } } +// let your_order = Order{ +// name: String::from("Hacker in Rust"), +// count: 1, +// ..order_template //dot dot operator meaning the rest of struct fields remains the same +// }; + #[cfg(test)] mod tests { use super::*; @@ -34,20 +40,21 @@ 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; - // added the ampersand after the variable let_order to solve the compiler error msg below - // move occurs because `order_template` has type `Order`, which does not implement the `Copy` trait - let your_order = &order_template; - //assert_eq!(your_order.name, "Hacker in Rust"); - assert_eq!(your_order.name, "Bob"); + // this struct is moved from above to inside this function. + // create own struct version changing the name and count while the remaining fields remain the same + let your_order = Order{ + name: String::from("Hacker in Rust"), + count: 1, + ..order_template //dot dot operator meaning the rest of struct fields remain the same + }; + 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); assert_eq!(your_order.made_by_mobile, order_template.made_by_mobile); assert_eq!(your_order.made_by_email, order_template.made_by_email); assert_eq!(your_order.item_number, order_template.item_number); - //assert_eq!(your_order.count, 1); - assert_eq!(your_order.count, 0); + assert_eq!(your_order.count, 1); + //assert_eq!(your_order.count, 0); } }