shortened the code in traits/traits7.rs

This commit is contained in:
abhi3700 2023-04-07 20:46:41 +05:30
parent 58ccf4d958
commit 29388557f8
2 changed files with 14 additions and 10 deletions

View File

@ -6,28 +6,24 @@
// I AM NOT DONE // I AM NOT DONE
#[allow(unused_imports)] use std::fmt::{Debug};
use std::fmt::{Debug, Formatter};
/// Struct representing a house /// Struct representing a house
// TODO: Complete the Code #[derive(Default)]
#[derive(??, Default)]
struct House { struct House {
area_sqft: u32, area_sqft: u32,
purchase_date: String, purchase_date: String,
} }
/// Struct representing a vehicle /// Struct representing a vehicle
// TODO: Complete the Code #[derive(Default)]
#[derive(??, Default)]
struct Vehicle { struct Vehicle {
name: String, name: String,
model: String, model: String,
purchase_date: String, purchase_date: String,
} }
// TODO: Complete the code trait Details {
trait Details: ?? {
fn summary(&self) -> String; 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() { pub fn main() {
let x = foo(true); let x = foo(true);
println!("{:?}", x); println!("{:?}", x);

View File

@ -754,8 +754,9 @@ path = "exercises/traits/traits7.rs"
mode = "compile" mode = "compile"
hint = """ hint = """
To make the function printable on console using `:?` symbol. Use the `Debug` trait for the `Details` trait. 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, Here, the trait object has to implement `Debug` trait, otherwise the function returning trait object will
otherwise the function returning trait object will not be printable on console. not be printable on console using {:?} symbol.
See the documentation at: https://doc.rust-lang.org/std/fmt/trait.Debug.html See the documentation at: https://doc.rust-lang.org/std/fmt/trait.Debug.html
""" """