知识卡片
调试格式与展示契约
{:#?} 和 {:?} 使用 Debug,结构体可显式派生它。{} 使用 Display,如何选择字段、单位和文案通常依赖产品语义,需要单独约定。调试表示适合观察对象,避免把它当稳定的机器接口。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 用结构体组织相关数据 An Example Program Using Structs · 查看原书依据
The println! macro can do many kinds of formatting, and by default, the curly brackets tell println! to use formatting known as Display: output intended for direct end user consumption. The primitive types we’ve seen so far implement Display by default because there’s only one way you’d want to show a 1 or any other primitive type to a user. But with structs, the way println!
参考来源
- 位置:《The Rust Programming Language(第2版)》第05章 用结构体组织相关数据(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c05.xhtml`,块 0093)
- 结论依据:书中用 Rectangle 缺少 Display 的诊断说明开发输出与用户格式用途不同。
- 原始内容:The println! macro can do many kinds of formatting, and by default, the curly brackets tell println! to use formatting known as Display: output intended for direct end user consumption. The primitive types we’ve seen so far implement Display by default because there’s only one way you’d want to show a 1 or any other primitive type to a user. But with structs, the way println!