Expand description
debug2
is a pretty printing crate based on std::fmt
Why not just use Debug
The Debug
trait is good, but the problem is it is not very good at nested stuctures.
Either you use {:?}
and get a line that is too long, or too many lines with not enough
information on them.
let complex_structure = vec![
vec![Some(1), Some(2), Some(3), None],
vec![Some(2), None],
vec![Some(4), Some(7)],
vec![Some(1), Some(2), Some(3), None],
];
let one_line = format!("{:?}", complex_structure);
assert_eq!(one_line, "[[Some(1), Some(2), Some(3), None], [Some(2), None], [Some(4), Some(7)], [Some(1), Some(2), Some(3), None]]");
let many_lines = format!("{:#?}", complex_structure);
assert_eq!(many_lines, "[
[
Some(
1,
),
Some(
2,
),
Some(
3,
),
None,
],
[
Some(
2,
),
None,
],
[
Some(
4,
),
Some(
7,
),
],
[
Some(
1,
),
Some(
2,
),
Some(
3,
),
None,
],
]")
pprint
aims to be a third alternative, that gets this correct.
use debug2::pprint;
let complex_structure = vec![
vec![Some(1), Some(2), Some(3), None],
vec![Some(2), None],
vec![Some(4), Some(7)],
vec![Some(1), Some(2), Some(3), None],
vec![Some(2), None],
vec![Some(4), Some(7)],
vec![Some(1), Some(2), Some(3), None],
vec![Some(2), None],
vec![Some(4), Some(7)],
];
assert_eq!(
pprint(complex_structure),
"\
[
[Some(1), Some(2), Some(3), None],
[Some(2), None],
[Some(4), Some(7)],
[Some(1), Some(2), Some(3), None],
[Some(2), None],
[Some(4), Some(7)],
[Some(1), Some(2), Some(3), None],
[Some(2), None],
[Some(4), Some(7)],
]"
);
To use, derive Debug
for your types, and then use pprint
to print them.
You can also manually implement Debug
, using a subset of the API in std::fmt::Formatter
Limitations
- Speed: While doing this will always mean extra work, this crate is paticularly inefficient.
- Prevalence: Almost every type implements
std::fmt::Debug
, but not this type - The derive isn’t great: The derive macro for
std::fmt::Debug
works everywhere. This one is kind of basic, and will probably not work everywhere it should.
Macros
Prints and returns the value of a given expression for quick and dirty debugging.
Structs
A struct to help with Debug
implementations.
A struct to help with Debug
implementations.
Configuration for formatting.
Traits
Pretty Printed Formatting
Functions
Pretty Print an item to a string
Pretty Print an item to a string, or return an error