Consider the following piece of code:
use std::fmt::Write;
struct X;
impl std::fmt::Display for X {
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
f.write_char('X')
}
}
fn main() {
let x = X{};
println!("{}", x.to_string());
}
Why does implementing Display
for a struct add the ability to call to_string()
on it? The only method provided by Display
is fmt()
, so there has to be something else going on under the covers.