Formatted strings and Debug


#1

It’s a bug that these aren’t both space padded, right? Is there some even more mysterious syntax for formatting Debug-strings?

println!("[ {:>10} ]", "dog");
println!("[ {:>10?} ]", "dog");

Output:

[        dog ]
[ "dog" ]

Here of course is the workaround, such as it is.

println!("[ {:>10} ]", format!("{:?}", "dog"));

#2

I don’t think it is a bug. The Debug output is for inspecting stuff while debugging. It’s not meant to be part of the actual program, thus other formatting options aren’t implemented.


#3

Either it should do padding, or it should reject the format string as invalid. Either way, there’s a problem in there somewhere.


#4

Agreed, there should be a compile-time error.


#5

I’m glad we can agree at least that there has to be a problem somewhere. However I don’t subscribe to the notion that I am less worthy of properly formatted output than are others. Given that there is a direct mechanical translation possible as seen in the workaround, I cannot fathom why one would take the position that this SHOULD be a compile-time error. A compile time error is less broken than simply ignoring the formatting string, yes, but it’s still broken.

This has already been acknowledged as a “known issue” but the response is discouraging:

This is a known issue with implementations of Debug and Display, and unfortunately there is no real way to statically enforce that all formatting flags are respected while also maintaining efficiency. You’re always able to allocate an intermediate String and then apply the formatting flags to that but otherwise requiring implementations to respect these flags would hamstring the performance.

Let’s separate the question of static enforcement of the formatting flags from the implementation of them. The first is a luxury, the second is simple correctness. So the comment is initially non-responsive: static enforcement of the flags is not the issue. The second sentence makes the problem seem much worse than I’d realized, since it’s not limited to Debug. The implication is that the correctness of string composition is dependent on the explicit cooperation of every implementation which processes a value. Probably I’m misunderstanding something because it’s hard to believe it would be designed that way.

I guess there’s always sprintf.