28

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:

Those damn crates

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?

Cargo.lock

[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"
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
erip
  • 16,374
  • 11
  • 66
  • 121

2 Answers2

47

I found the answer: cargo doc --no-deps.

erip
  • 16,374
  • 11
  • 66
  • 121
  • 3
    Thank you soo much! Didn't see that anywhere. That command is unfortunately counterintuitive. I would expect `cargo do` without deps, and `cargo doc --with-deps` with deps – yerlilbilgin Apr 09 '21 at 12:46
  • 9
    If you already generated your documentation before adding the `--no-deps` flag, you'll need to delete the `/target/doc` directory and regenerate the docs to make it purge the dependencies that show up on the page. I was confused why this wasn't working until I tried that. – Keavon Jan 14 '22 at 21:17
  • 1
    This last comment helped me, strange that changing the settings for documentation generation doesn't rebuild the entire doc. – SeedyROM Jul 16 '22 at 11:16
  • I'd like to show my dependencies, ie crates from the same workspace, but no third party deps. Is that possible? – Anatoly Bugakov Jun 27 '23 at 14:38
3

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:

Example of documentation

NVRM
  • 11,480
  • 1
  • 88
  • 87
Gonçalo Peres
  • 11,752
  • 3
  • 54
  • 83