Question:
How to print structs and arrays? – how does one pretty print a rust struct or any data type?Sure, one can write the custom Debug method. But is there some way which enables the print by default?
One option is to use: https://docs.rs/pretty-trait/latest/pretty_trait/
Answer:
When you implementDebug
, Rust provides “pretty printing” with {:#?}
. From the std::fmt
documentation:# – This flag indicates that the “alternate” form of printing should be used. The alternate forms are: {:#?} – pretty-print the Debug formatting (adds linebreaks and indentation) [others omitted]
Example:
If you have better answer, please add a comment about this, thank you!