25

我刚刚开始使用 Rust,并试图为我编写的代码生成文档。当我发布时cargo doc,我看到了一些奇怪的东西。

21:53 $ cargo doc
   Compiling regex-syntax v0.2.2
   Compiling libc v0.2.2
   Compiling memchr v0.1.7
   Compiling aho-corasick v0.3.4
   Compiling regex v0.1.41
   Compiling my_project v0.0.1 (path/to/my_project)

当我打开时my_project/target/doc/my_project/index.html,我注意到所有依赖项都包含在我的文档中:

那些该死的箱子

我希望对用户隐藏这些依赖项的文档,因此我的文档仅显示如何使用我的代码。

我怎样才能做到这一点?

货锁

[root]
name = "my_project"
version = "0.0.1"
dependencies = [
 "regex 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "aho-corasick"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "memchr 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "libc"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "memchr"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "libc 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "regex"
version = "0.1.41"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "aho-corasick 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
 "memchr 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
 "regex-syntax 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "regex-syntax"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
4

2 回答 2

38

我找到了答案:cargo doc --no-deps

于 2015-12-11T03:16:28.437 回答
1

默认情况下,cargo doc为本地包和所有依赖项构建文档。输出以 rustdoc 的常用格式放置在 target/doc 中。

为避免为依赖项构建文档,请传递--no-deps.

通常,我也倾向于通过--open。这将在构建文档后在浏览器中打开文档。

cargo doc --no-deps --open

在这里可以找到有关如何构建包文档的更多详细信息。


简单的例子。考虑以下lib.rs文件

//! This is my module documentation. My library is so nice!

/// four() is a function that returns `4`
///
/// ````
/// use mylib::four;
/// let x = four();
/// assert_eq!(four(), 4);
/// ````
pub fn four() -> i32 { 4 }

#[cfg(test)]
mod tests {
    use super::four;
    #[test]
    fn it_works() {
        assert_eq!(four(), 4);
    }
}

当一个人跑

cargo doc --no-deps --open

浏览器打开以下内容:

文档示例

于 2021-09-15T10:44:07.757 回答