有些情况下,stderr是不可用的。是否有一种方法可以让dbg!()打印到stdout,或者有其他命令将与dbg!()相同的信息打印到stdout?
发布于 2022-02-21 15:31:09
对于另一个命令,复制并粘贴implementation of dbg,将eprintln更改为println,将$crate更改为::std。
macro_rules! dbg {
// NOTE: We cannot use `concat!` to make a static string as a format argument
// of `println!` because `file!` could contain a `{` or
// `$val` expression could be a block (`{ .. }`), in which case the `println!`
// will be malformed.
() => {
::std::println!("[{}:{}]", ::std::file!(), ::std::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 => {
::std::println!("[{}:{}] {} = {:#?}",
::std::file!(), ::std::line!(), ::std::stringify!($val), &tmp);
tmp
}
}
};
($($val:expr),+ $(,)?) => {
($(::std::dbg!($val)),+,)
};
}
fn main() {
dbg!(1 + 1);
}https://stackoverflow.com/questions/71208839
复制相似问题