The dbg! macro was just stabilized and will be in Rust 1.32 (see https://github.com/rust-lang/rust/issues/54306). Since one important reason for introducing the macro in the first place was to help beginners it might make sense to use it in the book.
A lot of our printlns would be dbgs, I think. @carols10cents should we do a massive switch-over?
As a new rust user I really like dbg. I think it would also make the introductory sections of the book clearer because you don't need to gloss over the string formatting rules or explain what "{}" means.
Sure, once we switch to 1.32 i'd be up for it. We're currently on 1.31.
since there is code that need 1.32 or newer (#2034) I think the title page should be updated and dbg! can come.
Just realized that because dbg! takes ownership, instead of having to explain {} or {:?}, we'll have to explain & in order to use this early on in the book. 馃し鈥嶁檧 Not sure that's an improvement.
It can be used on copy types without that. Or save dbg! for just after borrowing is addressed?
@carols10cents dbg! return its parameter, so you can use it when assigning a variable or you can shadowing it.
fn main() {
let s = dbg!("abc".to_owned());
let s = dbg!(s);
drop(s);
}
Or even
fn main() {
if "42" == dbg!(answer.get()) {
println!("The answer is 42");
}
}
Copy types always work (as @dekellum sayed)
@rusty-snake yes, I read the 2 RFCs and the tracking issue before commenting here. I understand the justification behind the dbg! macro taking ownership. I don't think that affects the issue I raised here over whether it would be better to switch the book from using println! to dbg!.
Isn't there intrinsic value to the reader and early learner, to see both println! and dbg! at some point in the book, say before finding it in code in the wild?
@dekellum the issue is that closing this issue is not just a quick find-and-replace println! for dbg! in the book, and that the implementation of dbg! complicates the explanation.
Fair enough, I still think it has intrinsic value, as early learners will soon encounter both in the wild, and print debugging is rather fundamental.
I've been going thru the book, as a newbie, and was already thinking that printing debug info wouldn't be as fun as I'd like.
Then I just ran across dbg! which is indeed fabulous.
I thing getting at least a mention of it in the documentation is a great idea.
We don't have to switch everything to use it in a few places and then later start using it more after & has been explained.
Most helpful comment
As a new rust user I really like
dbg. I think it would also make the introductory sections of the book clearer because you don't need to gloss over the string formatting rules or explain what"{}"means.