The dbg macro lacks a possibility to format the output. For instance, it is not possible to print an u64 as hexadecimal. Therefore, I was thinking about below macro that, when given more than one expression, does allow formatting:
#[macro_export]
macro_rules! dbg {
($expr:expr) => (dbg!("{:#?}", $expr));
($fmt:expr, $expr:expr$(, $opt:expr)*) => {
match $expr {
expr => {
eprintln!(concat!("[{}:{}] {} = ", $fmt), file!(), line!(), stringify!($expr), &expr$(, $opt)*);
expr
}
}
}
}
With a single argument this behaves like current dbg, In case there are 2 or more, the first argument is a string literal format string, the second is the evaluated, optional remaining arguments can be provided as specified in the format string.
let t = dbg!(2 + 1);
// instead of:
let _: u64 = dbg!((1 << 63) | (t << 8));
// this gives a nicer representation:
let _: u64 = dbg!("0x{:x}", (1 << 63) | (t << 8));
// also possible:
let _: u64 = dbg!("0x{:x}, with t={}", (1 << 63) | (t << 8), t);