I've just started playing with Rust and was trying to generate docs for the code I've written. When I issued cargo doc
, I saw something a little weird.
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)
When I opened my_project/target/doc/my_project/index.html
, I noticed that all of the dependencies were included in my docs:
I'd like these dependencies' documentations to be hidden from the user so my documentation only shows how to use my code.
How can I do this?
[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"
What is rustdoc? The standard Rust distribution ships with a tool called rustdoc . Its job is to generate documentation for Rust projects. On a fundamental level, Rustdoc takes as an argument either a crate root or a Markdown file, and produces HTML, CSS, and JavaScript.
When no target selection options are given, cargo doc will document all binary and library targets of the selected package. The binary will be skipped if its name is the same as the lib target. Binaries are skipped if they have required-features that are missing.
Windows: %USERPROFILE%\. cargo\config. toml.
I found the answer: cargo doc --no-deps
.
By default, cargo doc
builds the documentation for the local package and all dependencies. The output is placed in target/doc in the rustdoc's usual format.
To avoid to build the documentation for the dependencies, pass --no-deps
.
Usually, I tend to pass --open
as well. This opens the documentation in a browser after building them.
cargo doc --no-deps --open
Here one can find more details on how to build a package's documentation.
Simple example. Considering the following lib.rs
file
//! 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); } }
When one runs
cargo doc --no-deps --open
The browser opens up with the following:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With