文档
使用 cargo doc
在 target/doc
中构建文档,cargo doc --open
将自动在您的 Web 浏览器中打开它。
使用 cargo test
运行所有测试(包括文档测试),使用 cargo test --doc
仅运行文档测试。
这些命令将根据需要适当调用 rustdoc
(和 rustc
)。
文档注释
文档注释对于需要文档的大型项目非常有用。当运行 rustdoc
时,这些注释会被编译成文档。它们用 ///
表示,并支持 Markdown。
要运行测试,首先将代码构建为库,然后告诉 rustdoc
在哪里找到该库,以便它可以将其链接到每个 doctest 程序中
$ rustc doc.rs --crate-type lib
$ rustdoc --test --extern doc="libdoc.rlib" doc.rs
文档属性
以下是一些与 rustdoc
一起使用的最常见的 #[doc]
属性示例。
inline
用于内联文档,而不是链接到单独的页面。
#[doc(inline)]
pub use bar::Bar;
/// bar docs
pub mod bar {
/// the docs for Bar
pub struct Bar;
}
no_inline
用于防止链接到单独的页面或任何地方。
// Example from libcore/prelude
#[doc(no_inline)]
pub use crate::mem::drop;
hidden
使用此属性告诉 rustdoc
不要将其包含在文档中
对于文档,rustdoc
被社区广泛使用。它被用来生成 std 库文档。