The current dbg!() implementation lacks formatting which makes it less convenient as debug output for bit masks, floating points and other structures. The original dbg implementation also missed formatting options, was not accepted, therefore it is not a valid argument per se to state that my implementation is not in compliance with it. There is room for improvement.
The multi-parameter in the original proposed dbg!() seems unnecessary to me because the evaluated argument can be passed as tuple.
let (i, j) = dbg!((3, 5));
let (k, l) = dbg!("{:?}", (3, 5));
// and if you really want to, you could still evaluate the 2nd argument of a tuple:
if dbg!("{:?}", (3, _j == 5)).1 {
dbg!("J was 5");
}
I’ve made a few changes in my implementation (below) that allows using dbg!() without argument (just prints file:line) or with a string literal to print a notice, these use cases seem intuitive to me as well.
As mentioned dbg!() with a single expression behaves as current dbg!(). there are certain variables that are printed more nicely with formatting, this I try to show in my main() function.
dbg_macro_playground
#[macro_export]
macro_rules! dbg {
() => (eprint!("[{}:{}]\n", file!(), line!()));
($l:literal) => (dbg!("{}", $l));
($expr:expr) => (dbg!(concat!(stringify!($expr), " = {:#?}"), $expr));
($fmt:expr, $expr:expr$(, $opt:expr)*) => {
match $expr {
expr => {
eprint!("[{}:{}] ", file!(), line!());
eprint!(concat!($fmt, "\n"), &expr$(, $opt)*);
expr
}
}
}
}