如何将 dbg!() 重定向到标准输出?

How can I redirect dbg!() to stdout?

有些情况下 stderr 不可用。有没有办法让 dbg!() 打印到 stdout 或将与 dbg!() 相同的信息打印到 stdout 的替代命令?

对于替代命令,copy-and-paste 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 - 
        match $val {
            tmp => {
                ::std::println!("[{}:{}] {} = {:#?}",
                    ::std::file!(), ::std::line!(), ::std::stringify!($val), &tmp);
                tmp
            }
        }
    };
    ($($val:expr),+ $(,)?) => {
        ($(::std::dbg!($val)),+,)
    };
}

fn main() {
    dbg!(1 + 1);
}