pub trait Debug {
// Required method
fn fmt(&self, f: &mut Formatter<'_>) -> Result;
}Expand description
Pretty Printed Formatting
This is much like std::fmt::Debug, but it supports much better multiline output
§Examples
use debug2::{pprint, Debug};
#[derive(Debug)]
struct Numbers {
a: Vec<Vec<i32>>,
b: String,
}
let a = Numbers {
a: vec![vec![10; 10]; 2],
b: "FooBar".to_owned(),
};
assert_eq!(
pprint(&a),
"\
Numbers {
a: [
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10],
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10],
],
b: \"FooBar\",
}"
);You can also implement fmt manually, using an API much like std::fmt::Formatter
use debug2::{pprint, Debug, Formatter};
use std::fmt;
struct Chunked10([u8; 100]);
impl Debug for Chunked10 {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.0.chunks(10)).finish()
}
}
assert_eq!(
pprint(Chunked10([0; 100])),
"\
[
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
]"
);Required Methods§
Sourcefn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Formats the value using the given formatter.
Note that this may be called more than once for any invocation of pprint, if you do
side effects in this, make sure they are idempotent. In general, don’t relly on how often
this function is called, as it may change in a future release.