← 返回刚才阅读的位置

知识卡片

普通读书笔记卡 · 6075.c.4 · 生成模型:未记录

dbg返回表达式的值

理解等级:中级

dbg!(expr) 把表达式和值写到标准错误,然后返回该值,便于临时观察计算。传入拥有值时会经过移动语义,如果只是查看对象,可以传入 &value。println! 通常走标准输出,适用的输出契约不同。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • 用结构体组织相关数据 An Example Program Using Structs · 查看原书依据

      Another way to print out a value using the Debug format is to use the dbg! macro, which takes ownership of an expression (as opposed to println!, which takes a reference), prints the file and line number of where that dbg! macro call occurs in your code along with the resultant value of that expression, and returns ownership of the value.

    参考来源

    - 位置:《The Rust Programming Language(第2版)》第05章 用结构体组织相关数据(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c05.xhtml`,块 0109) - 结论依据:原文说明 dbg! 输出调用位置和值,同时把表达式结果交回。 - 原始内容:Another way to print out a value using the Debug format is to use the dbg! macro, which takes ownership of an expression (as opposed to println!, which takes a reference), prints the file and line number of where that dbg! macro call occurs in your code along with the resultant value of that expression, and returns ownership of the value.