Skip to content

Rust Error E0433: Failed to Resolve, Use of Undeclared Type

E0433 means you wrote a path like HashMap::new() or rand::rng(), and Rust doesn’t know what the first part of it is. Almost always one line is missing: a use, a dependency in Cargo.toml, or a mod declaration. The good news is that rustc usually prints that exact line for you to copy. This page is part of the compiler errors guide.

error[E0433]: cannot find type `HashMap` in this scope
--> src/main.rs:2:21
|
2 | let mut stock = HashMap::new();
| ^^^^^^^ use of undeclared type `HashMap`
|
help: consider importing this struct
|
1 + use std::collections::HashMap;
|

This is the program that produces it:

fn main() {
let mut stock = HashMap::new();
stock.insert("apples", 12);
println!("{:?}", stock.get("apples"));
}

Older Rust versions print the same error as “failed to resolve: use of undeclared type HashMap”. Depending on what’s missing, the label can also say use of unresolved module or unlinked crate (for a crate or module) instead of use of undeclared type. All of these are E0433, and they are fixed the same way.

The ^^^^^^^ points at HashMap, the part before the ::. To call HashMap::new(), Rust first has to find something called HashMap. It looked at everything visible from line 2 and found nothing with that name, so it can’t even start looking for new.

The help: line is the important part. rustc searched the standard library, found a struct called HashMap in std::collections, and shows the use line that would make it visible. The 1 + means “add this as line 1”.

It’s like asking a librarian for “chapter 3 of that book” without saying which book. They know chapter 3 exists in plenty of books, but they need the title first.

Only a small set of names is visible in every Rust file automatically. This is called the prelude: Vec, String, Option, Some, None, Result, Ok, Err, Box and a few traits. Everything else, including HashMap, HashSet, fs and Ordering, has to be brought in with use or written with its full path.

That keeps the prelude small and avoids name clashes. The standard library has two different Ordering types, for example, and your own code may have a Duration or a Result too. Rust won’t guess which one you meant. You say it once at the top of the file, and every reader can see where each name comes from.

The same goes for crates and modules: a crate exists for your code only after it’s listed in Cargo.toml, and a module only after a mod line. See Modules and Cargo and crates.

Choose this when the help: says consider importing. It’s the most common fix by far. Copy the suggested line to the top of the file:

use std::collections::HashMap;
fn main() {
let mut stock = HashMap::new();
stock.insert("apples", 12);
println!("{:?}", stock.get("apples"));
}

Output:

Some(12)
▶ Try it Yourself

The other option is the full path at the call site, std::collections::HashMap::new(). That works too, but you’d have to repeat it everywhere, so a use line is the normal choice. More on this type in HashMaps.

If rustc offers several imports (consider importing one of these), read them before you pick one. They are different types that share a name, and only one of them is what you meant.

Fix 2: Fix the type’s name, or declare the type

Section titled “Fix 2: Fix the type’s name, or declare the type”

Choose this when the name is your own type. If there’s no help: at all, the type doesn’t exist yet; if there is a typo, rustc points at the similar name:

error[E0433]: cannot find type `Invetory` in this scope
--> src/main.rs:12:17
|
12 | let stock = Invetory::new();
| ^^^^^^^^ use of undeclared type `Invetory`
|
help: a struct with a similar name exists
|
12 | let stock = Inventory::new();
| +

The + under the line shows the missing letter. Fix the spelling:

struct Inventory {
items: Vec<String>,
}
impl Inventory {
fn new() -> Self {
Inventory { items: Vec::new() }
}
}
fn main() {
let stock = Inventory::new();
println!("{} items", stock.items.len());
}

Output:

0 items
▶ Try it Yourself

If Order::new() gives E0433 with no suggestion, you haven’t written struct Order yet (or it’s in a file you haven’t declared with mod). Also check the capital letters: inventory::new() and Inventory::new() are different names.

Choose this when the type or enum is defined inside a module and you use it from outside. Here the enum is in mod traffic, but main writes Light::Green:

error[E0433]: cannot find type `Light` in this scope
--> src/main.rs:11:15
|
11 | let now = Light::Green;
| ^^^^^ use of undeclared type `Light`
|
help: consider importing this enum
|
1 + use crate::traffic::Light;
|

Import the enum (or write traffic::Light::Green). Note that you import the enum, and then you still write Light::Green for each variant:

mod traffic {
#[derive(Debug)]
pub enum Light {
Red,
Yellow,
Green,
}
}
use crate::traffic::Light;
fn can_go(light: &Light) -> bool {
match light {
Light::Green => true,
Light::Yellow | Light::Red => false,
}
}
fn main() {
let now = Light::Green;
println!("{now:?}: go = {}", can_go(&now));
}

Output:

Green: go = true
▶ Try it Yourself

A variant path has exactly two parts: Enum::Variant. If you write one part too many, like Light::Red::Green, you get E0433 with cannot find module `Red` in `Light` and the label `Red` is a variant, not a module. See Enums.

Fix 4: Add the crate, or declare the module

Section titled “Fix 4: Add the crate, or declare the module”

Choose this when the label says use of unresolved module or unlinked crate. The first part of the path is a crate or a module that Rust doesn’t know about:

error[E0433]: cannot find module or crate `rand` in this scope
--> src/main.rs:4:19
|
4 | let mut rng = rand::rng();
| ^^^^ use of unresolved module or unlinked crate `rand`
|
= help: if you wanted to use a crate named `rand`, use `cargo add rand` to add it to your `Cargo.toml`

For a crate from crates.io, run cargo add rand in your project folder. For a standard library module like fs, rustc suggests use std::fs; instead. And for your own file, say src/inventory.rs, the help says to make use of source file src/inventory.rs, use `mod inventory` in this file to declare the module:

src/
├── main.rs inventory::count_items() ← needs `mod inventory;` at the top
└── inventory.rs pub fn count_items() -> u32 { 42 }

Here’s the same program in one file, with the module written inline so it runs on the Playground:

mod inventory {
pub fn count_items() -> u32 {
42
}
}
fn main() {
println!("Items in stock: {}", inventory::count_items());
}

Output:

Items in stock: 42
▶ Try it Yourself

With a separate file, the only change is that mod inventory { ... } becomes mod inventory; and the body moves to src/inventory.rs.

You may see extern crate rand; in old code. Since the 2018 edition you don’t need it: once a crate is in Cargo.toml, you can use it directly. Adding extern crate does not fix E0433. Without the dependency it only changes the error to E0463, can't find crate for rand. cargo add is the real fix.

Comparing with cmp. match a.cmp(&b) with Ordering::Less arms gives E0433, and rustc suggests two imports: use std::cmp::Ordering; and use std::sync::atomic::Ordering;. You want std::cmp::Ordering; the atomic one is for threads. See Match.

Tests in a mod tests block. A test module is a child module, so it can’t see your types without importing them. Inventory::new() inside mod tests gives E0433 with help: consider importing this struct and use crate::Inventory;. Fix: put use super::*; at the top of the test module. See Testing.

Code pasted from docs or blog posts. Examples often skip their use lines. fs::read_to_string(...) fails with cannot find module or crate `fs` in this scope and the help use std::fs;. Fix: add the import rustc shows, or look up the item’s path on its docs page.

  • E0432: the same kind of problem on a use line, unresolved import.
  • E0425: cannot find a value or function (no :: involved), like a misspelled variable. Current rustc also uses E0425 for a missing type in a type position, like let m: HashMap<&str, u32>; older versions call that E0412. The same use line fixes it.
  • E0599: the type was found, but the method or variant after :: doesn’t exist, like Light::Blue.
  • E0463: can't find crate, from extern crate without the dependency.
  • All compiler errors: the full list of error pages.

Quick check

1. Why does HashSet::new() give E0433 when Vec::new() works without any import?

2. rustc suggests both use std::cmp::Ordering; and use std::sync::atomic::Ordering; for your match a.cmp(&b). Which do you pick?

3. rand::rng() gives E0433 with the label 'use of unresolved module or unlinked crate'. What fixes it?

Score: 0 / 3