Skip to Content
E0433: Use of Undeclared Type

error[E0433]: failed to resolve: use of undeclared type

Rust can’t find a type, module or crate in the path you wrote. It isn’t imported with use, or the crate isn’t a dependency of your project.

Quick fix: Add the use line the compiler suggests (use std::collections::HashMap;), or add the crate with cargo add.

Code that triggers E0433

src/main.rs
fn main() { let mut ages = HashMap::new(); ages.insert("Ferris", 9); println!("{ages:?}"); }

What the compiler says

error[E0433]: failed to resolve: use of undeclared type `HashMap` --> src/main.rs:2:20 | 2 | let mut ages = HashMap::new(); | ^^^^^^^ use of undeclared type `HashMap` | help: consider importing this struct | 1 + use std::collections::HashMap; | error: aborting due to 1 previous error For more information about this error, try `rustc --explain E0433`.

Why Rust raises E0433

Rust only knows names that are defined in the current module, brought in with use, or part of the prelude, the small set of names every Rust file gets automatically. Vec, String and Option are in the prelude; HashMap isn’t, so it has to be imported.

For an external crate, the same code means the crate isn’t a dependency of your project.

Here rand::random is called without adding rand to Cargo.toml:

src/main.rs
fn main() { let roll: u8 = rand::random(); println!("{roll}"); }
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `rand` --> src/main.rs:2:20 | 2 | let roll: u8 = rand::random(); | ^^^^ use of unresolved module or unlinked crate `rand` | = help: you might be missing a crate named `rand` error: aborting due to 1 previous error For more information about this error, try `rustc --explain E0433`.

Run cargo add rand (or add it under [dependencies] in Cargo.toml) and the error goes away.

How to fix E0433

1. Import it with use

Add the import at the top of the file, exactly as the compiler’s help suggests.

src/main.rs
use std::collections::HashMap; fn main() { let mut ages = HashMap::new(); ages.insert("Ferris", 9); println!("{ages:?}"); }

Learn the concept behind it

The Packages, Crates and Modules lesson explains the rules behind E0433 with diagrams and more examples. For the official reference, run rustc --explain E0433 or read the E0433 entry in the Rust error index .

Hit a different error? Paste it into the Rust error lookup to find its fix.

Last updated on