知识卡片

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

可执行示例校验文档

理解等级:中级

在文档注释的 Rust 示例里附上预期结果,让 doc test 运行它。这样例子同时给读者演示接口,也能让 cargo test 捕捉 API 改动与示例失配。

相关卡片 [[公共接口文档写契约]]

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • 深入 Cargo 与 Crates.io Publishing a Crate to Crates.io · 查看原书依据

      Adding example code blocks in your documentation comments can help demonstrate how to use your library, and doing so has an additional bonus: running cargo test will run the code examples in your documentation as tests! Nothing is better than documentation with examples. But nothing is worse than examples that don’t work because the code has changed since the documentation was written.

    参考来源

    - 位置:《The Rust Programming Language(第2版)》第14章 深入 Cargo 与 Crates.io(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c14.xhtml`,块 0045) - 结论依据:原文以函数示例被 cargo test 执行、发现文档与实现不同步来说明用途。 - 原始内容:Adding example code blocks in your documentation comments can help demonstrate how to use your library, and doing so has an additional bonus: running cargo test will run the code examples in your documentation as tests! Nothing is better than documentation with examples. But nothing is worse than examples that don’t work because the code has changed since the documentation was written.