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
#[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);

View File

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