I'm trying to write some of my own debugging macros, and was looking at source code for rusts build in dbg!
:
macro_rules! dbg {
() => {
$crate::eprintln!("[{}:{}]", $crate::file!(), $crate::line!());
};
($val:expr $(,)?) => {
// Use of `match` here is intentional because it affects the lifetimes
// of temporaries - https://stackoverflow.com/a/48732525/1063961
match $val {
tmp => {
$crate::eprintln!("[{}:{}] {} = {:#?}",
$crate::file!(), $crate::line!(), $crate::stringify!($val), &tmp);
tmp
}
}
};
($($val:expr),+ $(,)?) => {
($($crate::dbg!($val)),+,)
};
}
Several things confuse me about this code:
- What is
$
operator peppered throughout this code doing? - What the plane language equivalent of
($val:expr $(,)?)
? I don't understand what the,
is and why it is there. - Why does the macro definition start with
() => {$crate::eprintln!("[{}:{}]", $crate::file!(), $crate::line!());};
?