-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a shorter macro for println!("{:?}", foo) #12015
Comments
You might be able to make it generic in the number of arguments with |
You can make it generic with something like: macro_rules! dump(
($($a:expr),*) => {
println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
}
) We've seen some recent pushback from including more |
@alexcrichton that seems mostly orthogonal. Any opinion on whether this, a macro for quick print-style debugging, is something we want at all? |
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 |
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 This is all personal opinions of course, but anything that is not in the prelude (or equivalent) doesn’t cut it for me. |
A default format string of Poly {:?} is generated to match the number of passed arguments. This propagates to all fmt macros, such as println!, debug!, format!, write!, etc. Example: println!("{:?}", value); => println!(value); fixes rust-lang#12015
dump! is a shorter way of quickly outputing some values to debug programs. Example: dump!(val_a, 2 + 2); => "val_a = true, 2 + 2 = 4" fixes rust-lang#12015
nice, I have the same exact macro myself, except i add "{}:{}", file!(),line!(), ... and display the expressions eg |
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 |
How about |
tiny precedence: |
I'd be willing to make a PR if that's the right thing to do in this situation? |
@reem, rather just than provide implementation, what’s needed now is convince the core team that this is a good idea. @alexcrichton disagrees, so far. Personally, I think that even having to go back to the crate top-level to add |
I tend to agree with you. I have started using this macro in my code for debugging and it is extremely useful, but it's pretty bothersome to have to add extern crates just to use it, then explicitly avoid checking those in to version control. Having it available by default would be great. |
Given that |
@SimonSapin That's just more painful, to be honest. Now I'd like having a macro for this in the prelude even more. |
To clarify: I meant having a macro in the prelude that uses expands to |
Changes to the prelude now require a formal RFC. The |
@thestinger "memory unsafe", not safe, I'd assume :) |
Given the direction |
I recently changed rust-inspect to use {} too. |
Perhaps |
+1 for making |
Yes, that sounds more likely to be accepted. Anyone wants to write up an RFC? |
I submitted a PR doing that earlier in the year, it was rejected: |
@seanmonstar Yes, this was requested a couple of times already, but for some reason it keeps getting rejected by the core team. |
I’ve renamed this macro https://crates.io/crates/show |
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.) |
For those googling: |
[Documentation]: Update developer documentation link. Just noticed the docs links broken. The current doc link was broken. replaced with the updated version as found on the front page. Should the rest of the links be updated?
New Lint: [`thread_local_initializer_can_be_made_const`] Adds a new lint to suggest using `const` on `thread_local!` initializers that can be evaluated at compile time. Impl details: The lint relies on the expansion of `thread_local!`. For non const-labelled initializers, `thread_local!` produces a function called `__init` that lazily initializes the value. We check the function and decide whether the body can be const. If so, we lint that the initializer value can be made const. changelog: new lint [`thread_local_initializer_can_be_made_const`] fixes: rust-lang#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?
The text was updated successfully, but these errors were encountered: