知识卡片

普通读书笔记卡 · 743.d.2.b.5.a.2.a.5 · 生成模型:GPT-6

文档示例随宏一起测试

理解等级:中级

可分发过程宏的文档可以分为条目级和 crate 级。宏定义上的 /// 描述具体调用,lib.rs 的 //! 描述整个 crate,还可以用 #![doc = include_str!("../README.md")] 将 README 合入 crate 文档,减少两份说明分开更新的成本。

Rust 代码块可以作为 doctest 随 cargo test 运行。例子里要导入宏,并保证示例使用的配置路径在测试环境可找到。按 feature 隐藏的宏默认可能不会出现在文档里,书中示范为文档构建加入 doc 条件,也应对其导入与实现使用相容条件,并检查对应 feature 的 doctest。

相关卡片 [[用特性控制宏的导出]]。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • Macros and the outside world rustdoc 的条目、crate 与样例 · 查看原书依据

      Because these tests are run when you do a cargo test, you should make sure they actually work—hence the import and the specific configuration path, since lib.rs is in a different location compared to my usage examples. And note the change I made to cfg. Without it, Rust did not generate documentation for this struct because it was hidden behind a feature. Adding the doc profile is one way to make it visible.

    参考来源

    - 位置:《Write Powerful Rust Macros》第10章《Macros and the outside world》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/chapter-10.xhtml#p87) - 结论依据:原书说明 doctest 要补真实导入和文件路径,并展示有条件的宏文档及 README 复用。 - 原始内容:Because these tests are run when you do a cargo test, you should make sure they actually work—hence the import and the specific configuration path, since lib.rs is in a different location compared to my usage examples. And note the change I made to cfg. Without it, Rust did not generate documentation for this struct because it was hidden behind a feature. Adding the doc profile is one way to make it visible.