Skip to content

feature: added exercise to "traits" chapter #1472

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions exercises/traits/traits6.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// 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

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,
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!("{}", ??);
}
15 changes: 15 additions & 0 deletions info.toml
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,21 @@ 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 = "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

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
"""

# QUIZ 3

[[exercises]]
Expand Down