From 58ccf4d9584c0b2e99bd55849e5feccc87bf811c Mon Sep 17 00:00:00 2001 From: abhi3700 Date: Fri, 7 Apr 2023 19:58:19 +0530 Subject: [PATCH 1/3] added exercises to \'traits\' chapter --- exercises/traits/traits6.rs | 111 ++++++++++++++++++++++++++++++++++++ exercises/traits/traits7.rs | 73 ++++++++++++++++++++++++ info.toml | 22 +++++++ 3 files changed, 206 insertions(+) create mode 100644 exercises/traits/traits6.rs create mode 100644 exercises/traits/traits7.rs diff --git a/exercises/traits/traits6.rs b/exercises/traits/traits6.rs new file mode 100644 index 0000000000..7c4da010bc --- /dev/null +++ b/exercises/traits/traits6.rs @@ -0,0 +1,111 @@ +// traits6.rs +// +// Your task is to replace the '??' sections so the code compiles. +// Don't change any line other than the marked one. +// Execute `rustlings hint traits6` or use the `hint` watch subcommand for a hint. + +// I AM NOT DONE + +struct House { + area_sqft: u32, + purchase_date: String, +} + +struct Vehicle { + name: String, + model: String, + purchase_date: String, +} + +trait Details { + fn summary(&self) -> String; +} + +impl Details for House { + fn summary(&self) -> String { + format!( + "The house has an area of {} sqft and was purchased on {}", + self.area_sqft, self.purchase_date + ) + } +} +impl Details for Vehicle { + fn summary(&self) -> String { + format!( + "The {} vehicle with model {} was purchased on {}", + self.name, self.model, self.purchase_date + ) + } +} + +// TODO: Complete the code +fn foo(flag: bool) -> ?? { + if flag { + Box::new(House { + area_sqft: 5000, + purchase_date: "21 Nov 2017".to_string(), + }) + } else { + Box::new(Vehicle { + name: "BMW".to_string(), + model: "320d".to_string(), + purchase_date: "13 Aug 2022".to_string(), + }) + } +} + +#[cfg(test)] +mod test { + + use super::*; + + fn init() -> (House, Vehicle) { + let house = House { + area_sqft: 5000, + purchase_date: "21 Nov 2017".to_string(), + }; + let vehicle = Vehicle { + name: "BMW".to_string(), + model: "320d".to_string(), + purchase_date: "13 Aug 2022".to_string(), + }; + + (house, vehicle) + } + + #[test] + fn check_foo_returns_house_if_true() { + let (house, _) = init(); + assert_eq!(house.summary(), foo(true).summary()); + } + + #[test] + fn check_foo_returns_vehicle_if_false() { + let (_, vehicle) = init(); + assert_eq!(vehicle.summary(), foo(false).summary()); + } + + #[test] + fn check_purchase_date_for_house() { + let (house, _) = init(); + assert_eq!( + format!( + "The house has an area of {} sqft and was purchased on {}", + house.area_sqft, house.purchase_date + ), + house.summary() + ); + } + + #[test] + fn check_purchase_date_for_vehicle() { + let (_, vehicle) = init(); + assert_eq!( + format!( + "The {} vehicle with model {} was purchased on {}", + vehicle.name, vehicle.model, vehicle.purchase_date + ), + vehicle.summary() + ); + } +} diff --git a/exercises/traits/traits7.rs b/exercises/traits/traits7.rs new file mode 100644 index 0000000000..ff05b33507 --- /dev/null +++ b/exercises/traits/traits7.rs @@ -0,0 +1,73 @@ +// traits7.rs +// +// Your task is to replace the '??' sections so the code compiles. +// Don't change any line other than the marked one. +// Execute `rustlings hint traits7` or use the `hint` watch subcommand for a hint. + +// I AM NOT DONE + +#[allow(unused_imports)] +use std::fmt::{Debug, Formatter}; + +/// Struct representing a house +// TODO: Complete the Code +#[derive(??, Default)] +struct House { + area_sqft: u32, + purchase_date: String, +} + +/// Struct representing a vehicle +// TODO: Complete the Code +#[derive(??, Default)] +struct Vehicle { + name: String, + model: String, + purchase_date: String, +} + +// TODO: Complete the code +trait Details: ?? { + fn summary(&self) -> String; +} + +impl Details for House { + fn summary(&self) -> String { + format!( + "The house has an area of {} sqft and was purchased on {}", + self.area_sqft, self.purchase_date + ) + } +} +impl Details for Vehicle { + fn summary(&self) -> String { + format!( + "The {} vehicle with model {} was purchased on {}", + self.name, self.model, self.purchase_date + ) + } +} + +// TODO: Complete the code +fn foo(flag: bool) -> ?? { + if flag { + Box::new(House { + area_sqft: 5000, + purchase_date: "21 Nov 2017".to_string(), + }) + } else { + Box::new(Vehicle { + name: "BMW".to_string(), + model: "320d".to_string(), + purchase_date: "13 Aug 2022".to_string(), + }) + } +} + +pub fn main() { + let x = foo(true); + println!("{:?}", x); + // TODO: Complete the code + // print the summary of the struct returned from the function `foo` + println!("{}", ??); +} diff --git a/info.toml b/info.toml index 28f9bb31e7..cc91a76b51 100644 --- a/info.toml +++ b/info.toml @@ -737,6 +737,28 @@ To ensure a parameter implements multiple traits use the '+ syntax'. Try replaci See the documentation at: https://doc.rust-lang.org/book/ch10-02-traits.html#specifying-multiple-trait-bounds-with-the--syntax """ +[[exercises]] +name = "traits6" +path = "exercises/traits/traits6.rs" +mode = "test" +hint = """ +To ensure a function is able to return incompatible types can be done by using a trait bound that is common to them. +Wrap the shared trait (implemented by the 2 struct types/objects) as a Boxed trait object. Try replacing the '??' with Box<_> + +See the documentation at: https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits +""" + +[[exercises]] +name = "traits7" +path = "exercises/traits/traits7.rs" +mode = "compile" +hint = """ +To make the function printable on console using `:?` symbol. Use the `Debug` trait for the `Details` trait. +Here, if the trait implements `Debug` trait then the corresponding struct types also has to implement `Debug` trait, +otherwise the function returning trait object will not be printable on console. +See the documentation at: https://doc.rust-lang.org/std/fmt/trait.Debug.html +""" + # QUIZ 3 [[exercises]] From 29388557f8d91f582c5986a89e5954896005204e Mon Sep 17 00:00:00 2001 From: abhi3700 Date: Fri, 7 Apr 2023 20:46:41 +0530 Subject: [PATCH 2/3] shortened the code in traits/traits7.rs --- exercises/traits/traits7.rs | 19 +++++++++++-------- info.toml | 5 +++-- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/exercises/traits/traits7.rs b/exercises/traits/traits7.rs index ff05b33507..56d42c9c32 100644 --- a/exercises/traits/traits7.rs +++ b/exercises/traits/traits7.rs @@ -6,28 +6,24 @@ // I AM NOT DONE -#[allow(unused_imports)] -use std::fmt::{Debug, Formatter}; +use std::fmt::{Debug}; /// Struct representing a house -// TODO: Complete the Code -#[derive(??, Default)] +#[derive(Default)] struct House { area_sqft: u32, purchase_date: String, } /// Struct representing a vehicle -// TODO: Complete the Code -#[derive(??, Default)] +#[derive(Default)] struct Vehicle { name: String, model: String, purchase_date: String, } -// TODO: Complete the code -trait Details: ?? { +trait Details { fn summary(&self) -> String; } @@ -64,6 +60,13 @@ fn foo(flag: bool) -> ?? { } } +impl Debug for dyn Details { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + // print the summary of the struct returned from the function `foo` + write!(f, "{}", ??) // TODO: Complete the code + } +} + pub fn main() { let x = foo(true); println!("{:?}", x); diff --git a/info.toml b/info.toml index cc91a76b51..6503bc759a 100644 --- a/info.toml +++ b/info.toml @@ -754,8 +754,9 @@ path = "exercises/traits/traits7.rs" mode = "compile" hint = """ To make the function printable on console using `:?` symbol. Use the `Debug` trait for the `Details` trait. -Here, if the trait implements `Debug` trait then the corresponding struct types also has to implement `Debug` trait, -otherwise the function returning trait object will not be printable on console. +Here, the trait object has to implement `Debug` trait, otherwise the function returning trait object will +not be printable on console using {:?} symbol. + See the documentation at: https://doc.rust-lang.org/std/fmt/trait.Debug.html """ From db8e9e85d65b2927e652f158c5cab92d0335ed07 Mon Sep 17 00:00:00 2001 From: abhi3700 Date: Sun, 9 Apr 2023 00:06:01 +0530 Subject: [PATCH 3/3] combined traits6 & traits7 into traits6 with compile --- exercises/traits/traits6.rs | 69 +++++++++------------------------ exercises/traits/traits7.rs | 76 ------------------------------------- info.toml | 12 +----- 3 files changed, 19 insertions(+), 138 deletions(-) delete mode 100644 exercises/traits/traits7.rs diff --git a/exercises/traits/traits6.rs b/exercises/traits/traits6.rs index 7c4da010bc..d955edea14 100644 --- a/exercises/traits/traits6.rs +++ b/exercises/traits/traits6.rs @@ -6,11 +6,17 @@ // I AM NOT DONE +use std::fmt::{self, Debug, Formatter}; + +/// Struct representing a house +#[derive(Default)] struct House { area_sqft: u32, purchase_date: String, } +/// Struct representing a vehicle +#[derive(Default)] struct Vehicle { name: String, model: String, @@ -54,58 +60,17 @@ fn foo(flag: bool) -> ?? { } } -#[cfg(test)] -mod test { - - use super::*; - - fn init() -> (House, Vehicle) { - let house = House { - area_sqft: 5000, - purchase_date: "21 Nov 2017".to_string(), - }; - let vehicle = Vehicle { - name: "BMW".to_string(), - model: "320d".to_string(), - purchase_date: "13 Aug 2022".to_string(), - }; - - (house, vehicle) - } - - #[test] - fn check_foo_returns_house_if_true() { - let (house, _) = init(); - assert_eq!(house.summary(), foo(true).summary()); - } - - #[test] - fn check_foo_returns_vehicle_if_false() { - let (_, vehicle) = init(); - assert_eq!(vehicle.summary(), foo(false).summary()); - } - - #[test] - fn check_purchase_date_for_house() { - let (house, _) = init(); - assert_eq!( - format!( - "The house has an area of {} sqft and was purchased on {}", - house.area_sqft, house.purchase_date - ), - house.summary() - ); +impl Debug for dyn Details { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + // print the summary of the struct returned from the function `foo` + write!(f, "{}", ??) // TODO: Complete the code } +} - #[test] - fn check_purchase_date_for_vehicle() { - let (_, vehicle) = init(); - assert_eq!( - format!( - "The {} vehicle with model {} was purchased on {}", - vehicle.name, vehicle.model, vehicle.purchase_date - ), - vehicle.summary() - ); - } +pub fn main() { + let x = foo(true); + println!("{:?}", x); + // TODO: Complete the code + // print the summary of the struct returned from the function `foo` + println!("{}", ??); } diff --git a/exercises/traits/traits7.rs b/exercises/traits/traits7.rs deleted file mode 100644 index 56d42c9c32..0000000000 --- a/exercises/traits/traits7.rs +++ /dev/null @@ -1,76 +0,0 @@ -// traits7.rs -// -// Your task is to replace the '??' sections so the code compiles. -// Don't change any line other than the marked one. -// Execute `rustlings hint traits7` or use the `hint` watch subcommand for a hint. - -// I AM NOT DONE - -use std::fmt::{Debug}; - -/// Struct representing a house -#[derive(Default)] -struct House { - area_sqft: u32, - purchase_date: String, -} - -/// Struct representing a vehicle -#[derive(Default)] -struct Vehicle { - name: String, - model: String, - purchase_date: String, -} - -trait Details { - fn summary(&self) -> String; -} - -impl Details for House { - fn summary(&self) -> String { - format!( - "The house has an area of {} sqft and was purchased on {}", - self.area_sqft, self.purchase_date - ) - } -} -impl Details for Vehicle { - fn summary(&self) -> String { - format!( - "The {} vehicle with model {} was purchased on {}", - self.name, self.model, self.purchase_date - ) - } -} - -// TODO: Complete the code -fn foo(flag: bool) -> ?? { - if flag { - Box::new(House { - area_sqft: 5000, - purchase_date: "21 Nov 2017".to_string(), - }) - } else { - Box::new(Vehicle { - name: "BMW".to_string(), - model: "320d".to_string(), - purchase_date: "13 Aug 2022".to_string(), - }) - } -} - -impl Debug for dyn Details { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - // print the summary of the struct returned from the function `foo` - write!(f, "{}", ??) // TODO: Complete the code - } -} - -pub fn main() { - let x = foo(true); - println!("{:?}", x); - // TODO: Complete the code - // print the summary of the struct returned from the function `foo` - println!("{}", ??); -} diff --git a/info.toml b/info.toml index 6503bc759a..b9332e7f19 100644 --- a/info.toml +++ b/info.toml @@ -740,23 +740,15 @@ See the documentation at: https://doc.rust-lang.org/book/ch10-02-traits.html#spe [[exercises]] name = "traits6" path = "exercises/traits/traits6.rs" -mode = "test" +mode = "compile" hint = """ To ensure a function is able to return incompatible types can be done by using a trait bound that is common to them. Wrap the shared trait (implemented by the 2 struct types/objects) as a Boxed trait object. Try replacing the '??' with Box<_> - See the documentation at: https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits -""" -[[exercises]] -name = "traits7" -path = "exercises/traits/traits7.rs" -mode = "compile" -hint = """ -To make the function printable on console using `:?` symbol. Use the `Debug` trait for the `Details` trait. +Also, in order to make the function printable on console using `:?` symbol. Use the `Debug` trait for the `Details` trait. Here, the trait object has to implement `Debug` trait, otherwise the function returning trait object will not be printable on console using {:?} symbol. - See the documentation at: https://doc.rust-lang.org/std/fmt/trait.Debug.html """