Debug vs production println

i initially came across this token

println!("{:?}", x);

and have searched if there are any others. turns out there are plenty of them. in tutorial videos and some online books, it is referred to as printing in debug info(if that makes sense)? and that printing in production you don't need it(?).

why there is a difference in printing? why mention debug versus production?

I have no idea exactly what your sources meant by debug versus production. Maybe they just meant that the Debug implementation is typically meant to display information for the coder, while the Display implementation is more likely aimed at the end user. Most types implement Debug, but many don't implement Display (e.g. Vec<_>, HashMap<_, _>). Derived Debug implementations aren't meant to be relied on as stable in terms of what exactly they output. Display cannot be derived (with the built in macros anyway) and is intended to be something you put more thought into, and perhaps even guarantees of stability on.

Anyway, there are a number of traits to display types in various ways beyond just those two. {} corresponds to Display, {:?} corresponds to Debug, {:p} corresponds to Pointer and so on. Other flags to control width, alignment, fill, and so on may also be present in the {...}, as documented on the rest of that page.

It's a little mini-language to learn for the sake of controlling output, ala printf or the like.

5 Likes

No, that's not what debug info is. Debug info is data that's placed by the compiler into the executable to aid debuggers making the code more readable while being inspected at runtime (e.g. by mapping the instruction counter to line numbers or memory addresses to variable names).

Whoever told you this was deeply confused. The Debug vs. Display format specifier has exactly nothing to do with whether you are running a development ("debug") or an optimized ("release") build. You can use both in both scenarios. They are completely orthogonal.

1 Like

Can you link us to an example of such a video or book? Then we might be able to see exactly what they said an where any confusion might be.

println always prints, like printf or cout in C, C++.

If you really want to print while debugging and not in production then you should use some logging library(crate) then you can write things like!

info!("Something happened")
debug!("Something else happened")

where it can be controlled whether the info message or debug message is printed by setting some log level environment variable at run time. See for example: https://crates.io/crates/log

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.