首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将dbg()重定向到stdout?

如何将dbg()重定向到stdout?
EN

Stack Overflow用户
提问于 2022-02-21 15:23:25
回答 1查看 200关注 0票数 0

有些情况下,stderr是不可用的。是否有一种方法可以让dbg!()打印到stdout,或者有其他命令将与dbg!()相同的信息打印到stdout?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-21 15:31:09

对于另一个命令,复制并粘贴implementation of dbg,将eprintln更改为println,将$crate更改为::std

代码语言:javascript
复制
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);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71208839

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档