You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add a shorter macro for println!("{:?}", foo) #12015
When print-debugging, I find myself writing println!("{:?}", some expression) a lot. I wish I didn’t have to write the "{:?}" part, which is always the same. For temporary debugging-only code, I don’t care about making the output pretty, I only want to see what’s going on.
So I’d like to have a new macro added wherever println!() is defined:
… with this pattern repeated to however many arguments is appropriate. (Although, worst case, one can always pass a tuple.) Or is there a way to write this completely generic over the number of arguments?
We've seen some recent pushback from including more {:?} instances in the standard library, so this may not have a great place in there (see #11793). A macro like this would be great for beginners, however.
I personally feel that a small debugging macro is something that'll be home-grown on a per-repo basis. The hard part right now is that macro_rules is behind a feature gate. My own personal opinion is to not include something like this in libstd, but to certainly document it in the macro tutorial or in the documentation somewhere (just as an example).
Print-style debugging IMO should be quick, ugly, and temporary. In particular, "temporary" means that I do not want debugging-only code to lie around in my code for longer than a few change/compile/test cycles. It should never end up in version control.
Given this, an example macro in the tutorial is really not that helpful, as println!("{:?}", foo) is still quicker.
This is all personal opinions of course, but anything that is not in the prelude (or equivalent) doesn’t cut it for me.
nice, I have the same exact macro myself, except i add "{}:{}", file!(),line!(), ... and display the expressions eg "blah.rs:120 foo=... bar=... " etc. really handy ,as one can just click in the debug output and jump to a source location in some editors.
I was inspired by this issue to make a tiny library providing just such a macro that will also insert the file and line name, along with the expressions you are evaluating next to their results. Available here.
Example:
let a = 7;let b = 4;debug!(a, b, a + b);// => file.rs - 3: a = 7, b = 4, a + b = 11
@reem that is pretty cool. I would be somewhat interested in seeing that in libdebug (although it would need a different name, to avoid conflicting with liblog's debug), I'm not sure if others agree.
@sfackler: Yeah, here's one example I didn't fix when I was working on this before: #9869
I no longer have any interest in fixing the dozen remaining memory safety holes and dealing with the other breakage... and apparently no one else does, since it hasn't really been touched since then.
Given the direction {:?} is going, if I were to write an RFC for this I’d now propose generating {} instead. But I suspect that RFC would be postponed for the same reason as rust-lang/rfcs#163 was, so I won’t bother writing it just yet. If anyone else wants to, though, feel free.
Perhaps println!(), format!(), debug!() &c. could simply be extended to allow eliding the format string, making println!(foo, bar) equivalent to println!("{} {}", foo, bar). This has the slight disadvantage of silencing the error for println!(foo, bar) where foo is a string literal intended as a dynamic format string, and instead creating a bug at runtime. It would also be backwards-compatible, although still probably require an RFC.
@seanmonstar Yes, this was requested a couple of times already, but for some reason it keeps getting rejected by the core team.
Maybe posting on Reddit and thus generating a wider debate on this would help? @alexcrichton Has you opinion changed on this in any way?
Splitting this into a separate macro would defeat the purpose of this, I think.
Activity
sfackler commentedon Feb 3, 2014
You might be able to make it generic in the number of arguments with
concat!. Writing it as a procedural macro would also do it.alexcrichton commentedon Feb 3, 2014
You can make it generic with something like:
We've seen some recent pushback from including more
{:?}instances in the standard library, so this may not have a great place in there (see #11793). A macro like this would be great for beginners, however.SimonSapin commentedon Feb 3, 2014
@alexcrichton that seems mostly orthogonal. Any opinion on whether this, a macro for quick print-style debugging, is something we want at all?
alexcrichton commentedon Feb 3, 2014
I personally feel that a small debugging macro is something that'll be home-grown on a per-repo basis. The hard part right now is that
macro_rulesis behind a feature gate. My own personal opinion is to not include something like this inlibstd, but to certainly document it in the macro tutorial or in the documentation somewhere (just as an example).SimonSapin commentedon Feb 3, 2014
Print-style debugging IMO should be quick, ugly, and temporary. In particular, "temporary" means that I do not want debugging-only code to lie around in my code for longer than a few change/compile/test cycles. It should never end up in version control.
Given this, an example macro in the tutorial is really not that helpful, as
println!("{:?}", foo)is still quicker.This is all personal opinions of course, but anything that is not in the prelude (or equivalent) doesn’t cut it for me.
format_args: allow omission of format string
dump!: debugging macro
dobkeratops commentedon Apr 22, 2014
nice, I have the same exact macro myself, except i add "{}:{}", file!(),line!(), ... and display the expressions eg
"blah.rs:120 foo=... bar=... "etc. really handy ,as one can just click in the debug output and jump to a source location in some editors.reem commentedon Jun 10, 2014
I was inspired by this issue to make a tiny library providing just such a macro that will also insert the file and line name, along with the expressions you are evaluating next to their results. Available here.
Example:
huonw commentedon Jun 10, 2014
@reem that is pretty cool. I would be somewhat interested in seeing that in libdebug (although it would need a different name, to avoid conflicting with liblog's
debug), I'm not sure if others agree.reem commentedon Jun 10, 2014
How about
inspect!, or perhapsdump!? I personally likeinspect!a bit more, just because it sounds a bit more professional :).seanmonstar commentedon Jun 10, 2014
tiny precedence:
util.inspectfrom nodejs, though it also colorizes and pretty prints jsonreem commentedon Jun 11, 2014
I'd be willing to make a PR if that's the right thing to do in this situation?
6 remaining items
sfackler commentedon Sep 19, 2014
@thestinger "memory unsafe", not safe, I'd assume :)
thestinger commentedon Sep 19, 2014
@sfackler: Yeah, here's one example I didn't fix when I was working on this before: #9869
I no longer have any interest in fixing the dozen remaining memory safety holes and dealing with the other breakage... and apparently no one else does, since it hasn't really been touched since then.
SimonSapin commentedon Sep 19, 2014
Given the direction
{:?}is going, if I were to write an RFC for this I’d now propose generating{}instead. But I suspect that RFC would be postponed for the same reason as rust-lang/rfcs#163 was, so I won’t bother writing it just yet. If anyone else wants to, though, feel free.reem commentedon Sep 19, 2014
I recently changed rust-inspect to use {} too.
ftxqxd commentedon Sep 19, 2014
Perhaps
println!(),format!(),debug!()&c. could simply be extended to allow eliding the format string, makingprintln!(foo, bar)equivalent toprintln!("{} {}", foo, bar). This has the slight disadvantage of silencing the error forprintln!(foo, bar)wherefoois a string literal intended as a dynamic format string, and instead creating a bug at runtime. It would also be backwards-compatible, although still probably require an RFC.MatejLach commentedon Sep 19, 2014
+1 for making
println!(foo, bar)equivalent toprintln!("{} {}", foo, bar)- would make things a lot less painful.SimonSapin commentedon Sep 19, 2014
Yes, that sounds more likely to be accepted. Anyone wants to write up an RFC?
seanmonstar commentedon Sep 19, 2014
I submitted a PR doing that earlier in the year, it was rejected:
#12384
MatejLach commentedon Sep 19, 2014
@seanmonstar Yes, this was requested a couple of times already, but for some reason it keeps getting rejected by the core team.
Maybe posting on Reddit and thus generating a wider debate on this would help?
@alexcrichton Has you opinion changed on this in any way?
Splitting this into a separate macro would defeat the purpose of this, I think.
SimonSapin commentedon Dec 10, 2014
I’ve renamed this macro
show!, changed it to use{}(now that{:?}doesn’t exist anymore), and uploaded it to crates.io:https://crates.io/crates/show
https://github.com/SimonSapin/rust-std-candidates#the-show-debugging-macro
ivan commentedon Feb 18, 2016
I've published a crate called dump that extends the above #12015 (comment) to also print the type for each variable.
(Update: If anyone uses it, I am looking for someone to adopt the repo and crate, please email me.)
codyps commentedon Oct 14, 2020
For those googling:
dbg!()in the standard library is probably what you wantAuto merge of rust-lang#12015 - arjo129:master, r=jonas-schievink
Auto merge of rust-lang#12026 - PartiallyTyped:12015, r=llogiq