Skip to content
cannot find crate '...'

cannot find crate '...'

DodaTech 3 min read

The “cannot find crate ‘…’” error in Rust means the compiler cannot locate a crate that you referenced with extern crate or through a use statement. This happens when the dependency is missing from your Cargo.toml file.

What It Means

Rust crates are packages of Rust code. When your code imports a crate, the compiler needs to know where to find it. Cargo resolves this by reading your Cargo.toml file, which lists all dependencies. If the requested crate is not listed, or the name does not match exactly, Rust cannot find it.

Why It Happens

  • The dependency is not listed in Cargo.toml under [dependencies].
  • The crate name in your code has a typo or uses the wrong case.
  • The crate was added to Cargo.toml but the version is wrong or incompatible.
  • The dependency is in the [dev-dependencies] section but is used in production code.
  • The crate has not been downloaded yet and the registry is unreachable.
  • The crate name differs from the package name (common with renamed crates).

How to Fix It

1. Add the dependency with cargo add

The easiest way is to use Cargo’s built-in command:

cargo add serde

This adds the latest version of serde to your [dependencies] in Cargo.toml.

2. Add the dependency manually

Edit your Cargo.toml file and add the crate under [dependencies]:

[package]
name = "myproject"
version = "0.1.0"

[dependencies]
serde = { version = "1.0", features = ["derive"] }
reqwest = "0.11"

3. Verify the crate name matches

Check the exact crate name on crates.io. A common mistake is using the display name instead of the package name:

// Wrong — crate is named "serde_json" not "serde-json"
use serde_json; // error if misspelled

// Correct
use serde_json; // matches the crate name exactly

4. Check the dependency section

If you added a dependency under [dev-dependencies], it is only available in test configurations:

[dev-dependencies]
tempfile = "3"   // only available in #[test] functions

[dependencies]
serde = "1.0"    // available everywhere

Move the dependency to [dependencies] if you need it in production code.

5. Update the registry and rebuild

If the crate should be available but is not being found, refresh the registry:

cargo update
cargo build

FAQ

What is the difference between a crate and a package?
In Rust, a crate is a compilation unit — either a binary or a library. A package is one or more crates defined by a Cargo.toml file. When you add a dependency, you are adding a package from crates.io that contains one or more crates.
Why does cargo add download files from the internet?
Cargo downloads crate source code from the crates.io registry. The source is compiled alongside your project. This ensures reproducibility and allows Cargo to resolve version conflicts automatically.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro