I would like a trait X to implement the Display trait so that it’s available for all structs that implement X.
How can I do ?
Thanks !
I tried this but got an error (in main()) :
use std::fmt;
pub trait MyTrait {
fn get_value(&self) -> i32;
}
impl fmt::Display for dyn MyTrait {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.get_value())
}
}
pub struct MyStruct {
value: i32,
}
impl MyTrait for MyStruct {
fn get_value(&self) -> i32 {
self.value
}
}
fn main() {
let my_struct = MyStruct { value: 42 };
println!("{}", my_struct); // `MyStruct` cannot be formatted with the default formatter
}