How do I prevent `rust doc` from adding dependencies to documentation?
Asked Answered
C

3

36

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"
Casualty answered 11/12, 2015 at 2:59 Comment(0)
C
56

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

Casualty answered 11/12, 2015 at 3:16 Comment(4)
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 depsDevoice
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.Southerland
This last comment helped me, strange that changing the settings for documentation generation doesn't rebuild the entire doc.Hash
I'd like to show my dependencies, ie crates from the same workspace, but no third party deps. Is that possible?Disequilibrium
A
5

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

Alcot answered 15/9, 2021 at 10:44 Comment(0)
S
0

I'm using Rust 1.79. What worked for me, to generate docs to just one crate in a workspace, is:

cargo doc --no-deps --package my_package

However, there is a caveat: if you don't generate docs for all the crates, the source link won't show – that is, the source code will be visible in the docs only if you include all crates in the docs.

Subservience answered 8/7 at 12:9 Comment(1)
You will get source links but only for items defined in the -p <package> you built the documentation for and not for items defined in other workspace packages. This is the same behavior as --no-deps with a path = dependency.Georgena

© 2022 - 2024 — McMap. All rights reserved.