iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
📝

Must-see for Rust Beginners! Learn Efficiently with `cargo doc --open`

に公開

I am a beginner who has recently been getting into Rust.
I would appreciate any comments if you find any mistakes or have suggestions for improvement 🙇

Now, to the main topic: how do you all look up the documentation for external libraries (crates) used in your projects?
Are you opening crates.io in your browser and searching, or hunting around for official websites?

Actually, there is an easier and more efficient way.
That is the cargo doc --open command that I will introduce in this article.

What is cargo doc --open?

cargo doc --open is a handy Cargo command that automatically generates documentation for all dependencies used in your current project and opens it in a browser.

Basic Usage

cargo doc --open

That's all it takes.

When you run the command...

  1. It analyzes the project's dependencies.
  2. It generates documentation for all crates locally.
  3. It automatically opens the documentation page in your browser.

What Makes It Convenient?

1. Works Offline

Once generated, the documentation is saved in the target/doc folder, so you can refer to it even without an internet connection.

2. Project-Specific Information

It displays documentation for the exact version you are using.
This eliminates the confusion of looking at the latest version online and thinking, "Wait, I can't find this method."

3. Dependencies Listed at a Glance

All crates used in your project are organized and displayed on a single page.
This is very handy when you need to browse related libraries across multiple crates.

4. Fast Access

Since it is generated locally, page loading is extremely fast.

Trying It Out

Preparation: Create a Sample Project

cargo new doc_sample
cd doc_sample

Add Dependencies to Cargo.toml

[dependencies]
serde = "1.0"
tokio = { version = "1.0", features = ["full"] }
reqwest = { version = "0.11", features = ["json"] }

Generate and Open Documentation

cargo doc --open

After running it, the browser will open a page similar to the following:

  • Documentation for serde
  • Documentation for tokio
  • Documentation for reqwest
  • All other transitive dependencies

More Useful Ways to Use It

Include Documentation for Your Own Code

cargo doc --open --document-private-items

This also generates documentation for private functions and modules.

Only Your Project Without Dependencies

cargo doc --open --no-deps

This generates documentation only for your own project, excluding external crates.

Only a Specific Package

cargo doc --open -p serde

This generates documentation only for the specified package.

Tips to Improve Learning Efficiency

1. Check Documentation Before Writing Code

Before using a new crate, first generate the documentation with cargo doc --open and get an overview of the API.

2. Check Documentation Immediately When an Error Occurs

When a compilation error occurs, you can quickly check the documentation of the relevant crate. It includes plenty of correct usage examples and sample code.

On the documentation page, you can also see a list of related crates, which can help you discover new useful libraries.

Cautions

Initial Generation Time

If your project has many dependencies, the initial documentation generation may take some time.

Disk Usage

The generated documentation is saved in target/doc and uses a considerable amount of disk space.

If you no longer need it, you can delete it with cargo clean.

Summary

cargo doc --open is an extremely useful command that can significantly boost the efficiency of your Rust learning.
I especially recommend it to beginners for the following reasons:

  • Reduced learning cost: Shortens the time spent searching in a browser.
  • Accurate information: Provides correct documentation corresponding to the version you are using.
  • Offline ready: Not affected by the internet environment, and eliminates the hassle of googling.

If you haven't used it yet, please give it a try.
You should be able to experience the excellent quality of Rust's development experience.


I hope this article helps you in your Rust learning journey.
If there are other useful Cargo commands, please feel free to leave a comment!

Discussion