1

I just created a new Rust project and introduced one dependency in main.rs. When I try to run rustdoc src/main.rs, I get the following error:

error[E0433]: failed to resolve: maybe a missing crate `rand`?
 --> src/main.rs:4:5

Even though Cargo.toml has that dependency already, it's installed and in Cargo.lock, it's imported in main.rs through use rand; and the project builds successfully, without any issues.

[package]
name = "test"
version = "0.1.0"
edition = "2021"

[dependencies]
rand = "0.8.5"

I saw some answers point to edition being absent or set to earlier versions as the cause; this is not the case here. I tried creating documents for fresh libraries or binaries as described in the documentation, but it works until I introduce a dependency, when the same error repeats.

Project structure (the rustdoc command is run from root):

├── Cargo.lock
├── Cargo.toml
├── readme.md
└── src
    └── main.rs

Any help is appreciated!

zarnoevic
  • 329
  • 3
  • 12
  • What happens when you run `cargo doc` from the root directory? If it works please add the output from `cargo doc --verbose`. – frankenapps Dec 30 '22 at 12:00
  • 4
    Are you running `rustdoc` directly? (You should be running `cargo doc`). – Chayim Friedman Dec 30 '22 at 12:02
  • Just to make the point even clearer: Your config file is named `Cargo.toml`, indicating that it needs `cargo` to resolve the dependencies ;) – Finomnis Dec 30 '22 at 12:13
  • For more info, look at the relevent section in [the rustdoc documentation](https://doc.rust-lang.org/rustdoc/what-is-rustdoc.html#using-rustdoc-with-cargo). – Finomnis Dec 30 '22 at 12:14
  • Almost a duplicate of https://stackoverflow.com/questions/69969831/missing-crate-when-using-rustc-but-not-using-cargo/69972075#69972075 (that one is about `rustc`, but the same applies to `rustdoc`). – Jmb Dec 30 '22 at 20:16

1 Answers1

1

To summarize the solution, cargo has an integration with rustdoc which solves the dependency problem automatically - the example given in the basic usage documentation only works when there are no dependencies. Just running cargo doc in the root will generate the wanted documentation, however under the target/doc path.

Thanks for the help!

zarnoevic
  • 329
  • 3
  • 12