Is it possible to define fmt::Display
and fmt::Debug
together, i.e. such that they use the same implementation?
Assume we have the below sample code (purely for demonstration purposes):
use std::fmt;
struct MyDate {
pub year: u16,
pub month: u8,
pub day: u8
}
impl PartialEq for MyDate {
fn eq(&self, other: &Self) -> bool {
self.year == other.year && self.month == other.month && self.day == other.day
}
}
impl fmt::Display for MyDate {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}-{:02}-{:02}", self.year, self.month, self.day)
}
}
impl fmt::Debug for MyDate {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self)
}
}
fn main() {
let d1 = MyDate { year: 2008, month: 9, day: 10 };
let d2 = MyDate { year: 2008, month: 9, day: 10 };
println!("Date = {}", d1); // requires fmt::Display
assert_eq!(d1, d2); // requires fmt::Debug
}
It seems that in order to print my class using println
and write unit tests using assert_eq
, I need to define both fmt
traits. Is there a way to have 1 "print" implementation that simultaneously defines fmt::Display
and fmt::Debug
? For example something along the lines of impl fmt::Debug, fmt::Display for MyDate { ... }
or similar?
Or is it common practice to just put #[derive(Debug)]
in front of any class? Apologies if my question is naïve, as I am new to rust.
fmt
for both traits at the same time, likeimpl fmt::Debug, fmt::Display for MyDate { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { ... } }
or similar? – Ecumenicist