Implement the Display
Trait
Source: https://doc.rust-lang.org/rust-by-example/hello/print/print_display.html
Deriving Debug
is convenient but oftentimes you want to customize how your custom type prints out.
You can implement the Display
trait to control that.
struct Ex { field1: i32, field2: i32, } impl std::fmt::Display for Ex { // You need to implement the following function. // You can use `write!()`, which is similar to // `println!()` but writes to a `Formatter`. // `write!()` returns the right type for `fmt()`. fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { write!(f, "field1: {}, field2: {}", self.field1, self.field2) } } fn main() { let ex = Ex { field1: 32, field2: 64 }; println!("{}", ex); }