Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download the documentation of a crate with Cargo?

In Haskell's Cabal, one can download the documentation for a package. Is it possible with Rust's Cargo? I searched on the Internet but found nothing.

like image 545
Xwtek Avatar asked Mar 05 '16 23:03

Xwtek


People also ask

Where are cargo dependencies stored?

registry/cache Downloaded dependencies are stored in the cache. The crates are compressed gzip archives named with a . crate extension.

What does cargo Doc do?

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.

What is crate in cargo?

A crate is the smallest amount of code that the Rust compiler considers at a time. Even if you run rustc rather than cargo and pass a single source code file (as we did all the way back in the “Writing and Running a Rust Program” section of Chapter 1), the compiler considers that file to be a crate.

Where are cargo binaries stored?

All binaries installed with cargo install are stored in the installation root's bin folder. If you installed Rust using rustup.rs and don't have any custom configurations, this directory will be $HOME/. cargo/bin. Ensure that directory is in your $PATH to be able to run programs you've installed with cargo install .


1 Answers

You can build the documentation for all crates that are currently specified in your Cargo.toml by using the command

cargo doc

A list of common Cargo commands can be found with cargo --help, and detailed information for a command can be found with cargo COMMAND --help:

$ cargo doc --help
cargo-doc 
Build a package's documentation

USAGE:
    cargo doc [OPTIONS]

OPTIONS:
    -q, --quiet                     No output printed to stdout
        --open                      Opens the docs in a browser after the operation
    -p, --package <SPEC>...         Package to document
        --all                       Document all packages in the workspace
        --exclude <SPEC>...         Exclude packages from the build
        --no-deps                   Don't build documentation for dependencies
        --document-private-items    Document private items
    -j, --jobs <N>                  Number of parallel jobs, defaults to # of CPUs
        --lib                       Document only this package's library
        --bin <NAME>...             Document only the specified binary
        --bins                      Document all binaries
        --release                   Build artifacts in release mode, with optimizations
        --features <FEATURES>       Space-separated list of features to activate
        --all-features              Activate all available features
        --no-default-features       Do not activate the `default` feature
        --target <TRIPLE>           Build for the target triple
        --target-dir <DIRECTORY>    Directory for all generated artifacts
        --manifest-path <PATH>      Path to Cargo.toml
        --message-format <FMT>      Error format [default: human]  [possible values: human, json, short]
    -v, --verbose                   Use verbose output (-vv very verbose/build.rs output)
        --color <WHEN>              Coloring: auto, always, never
        --frozen                    Require Cargo.lock and cache are up to date
        --locked                    Require Cargo.lock is up to date
        --offline                   Run without accessing the network
    -Z <FLAG>...                    Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details
    -h, --help                      Prints help information

By default the documentation for the local package and all dependencies is
built. The output is all placed in `target/doc` in rustdoc's usual format.

All packages in the workspace are documented if the `--all` flag is supplied. The
`--all` flag is automatically assumed for a virtual manifest.
Note that `--exclude` has to be specified in conjunction with the `--all` flag.

If the `--package` argument is given, then SPEC is a package ID specification
which indicates which package should be documented. If it is not given, then the
current package is documented. For more information on SPEC and its format, see
the `cargo help pkgid` command.

Especially useful to me is the --open flag, which opens the generated documentation in a browser.

I do not believe that there is any way to generate the documentation for an arbitrary package that you are not using. You could always create a new Cargo project, add the desired crates as dependencies, then follow the above steps.

like image 88
Shepmaster Avatar answered Oct 15 '22 03:10

Shepmaster