知识卡片

普通读书笔记卡 · 743.d.2.b.8.a

自定义输出复用标准格式参数

理解等级:中级

自定义日志或输出宏可把参数交给format_args!,复用Display、Debug和标准格式语法的编译期检查。再结合file!与line!加入调用位置,减少手工拼接与独有格式规则。类型检查不等于输出无敏感信息,仍应控制记录哪些值。

这张卡片看不懂?

已有反馈 · 0 / 10 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • Chapter 5. Tooling Formatting Values · 查看原书依据

      format_args! for macros that perform argument formatting rather than attempting to reinvent the wheel: /// Log an error including code location, with `format!`-like arguments. /// Real code would probably use the `log` crate. macro_rules! my_log { { $($arg:tt)+ } => { eprintln!("{}:{}: {}", file!(), line!(), format_args!($($arg)+)); } } let x = 10u8; // Form

    参考来源

    - 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第5章《Formatting Values》(spine9;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch05.html;u142-ch05-010)。 - 结论依据:原文在“format_args!”附近的定义、例子或代码支撑本卡核心说明:自定义日志或输出宏可把参数交给format_args!,复用Display、Debug和标准格式语法的编译期检查。边界按已读前提解释;示例未执行。 - 原始内容:format_args! for macros that perform argument formatting rather than attempting to reinvent the wheel: /// Log an error including code location, with `format!`-like arguments. /// Real code would probably use the `log` crate. macro_rules! my_log { { $($arg:tt)+ } => { eprintln!("{}:{}: {}", file!(), line!(), format_args!($($arg)+)); } } let x = 10u8; // Form