Is there a way to suppress pretty-printing when deriving Debug?

#1

I have some small structs/enums that I’m storing in vectors. I like being able to pretty-print values with the {:#?} format specifier, but I’d like to be able to print the vector on multiple lines, while each element takes just a single line.

That is, I’d like this code:

#[derive(Debug)]
struct Point { x: f64, y: f64 }
let points: Vec<Point> = unimplemented!();
println!("{:#?}", points);

to produce this:

[
    Point { x: 1.0, y: 1.0 },
    Point { x: 2.0, y: 4.0 }
]

Is there a way to derive Debug but suppress the alternate form when printing?

0 Likes

#2

I think a custom crate which provides derives could give you a customization like this, but the default derive doesn’t have it.

You can implement Debug manually and it shouldn’t be too hard to accomplish this there, even when using Formatter’s debug_struct.

0 Likes