在某些情況下 stderr 不可用。有沒有辦法dbg!()列印到標準輸出或列印與標準輸出相同的資訊的替代命令dbg!()?
uj5u.com熱心網友回復:
對于替代命令,復制并粘貼 的實作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://www.uj5u.com/qianduan/433735.html
