Error E0433

"failed to resolve: use of undeclared crate or module" — How to Fix

This error occurs when the compiler cannot find a crate or module you are trying to use, usually because it is missing from your `Cargo.toml`, not imported correctly, or the module path is wrong.

This error occurs when the compiler cannot find a crate or module you are trying to use, usually because it is missing from your Cargo.toml, not imported correctly, or the module path is wrong. To fix it, ensure the dependency is listed in Cargo.toml, run cargo build to fetch it, and verify you are using the correct use statement or module path relative to your crate root.

If you are trying to use an external library like serde, the most common mistake is forgetting to add it to the dependencies. Open your Cargo.toml and add the crate under the [dependencies] section. After saving, run cargo build to let Cargo download and compile the new dependency.

# Cargo.toml
[dependencies]
serde = { version = "1.0", features = ["derive"] }

Once the dependency is added, you must import it in your Rust source code. If you forget the use statement, the compiler will treat the name as undeclared even if the crate is installed.

// src/main.rs
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
struct User {
    name: String,
}

fn main() {
    let user = User { name: "Alice".to_string() };
    println!("{:?}", user);
}

If the error points to a local module (e.g., use my_module::something;), the issue is often file structure or visibility. Rust modules must match the file system hierarchy. If you have a file src/my_module.rs, you can reference it directly. If you have a directory src/my_module/, you must have a mod.rs file inside it or use the 2018 edition's implicit module discovery.

Ensure the module is declared in the parent scope. For a file src/utils.rs, add mod utils; in src/lib.rs or src/main.rs. If the module is private (the default), you cannot access it from outside the crate unless you mark it as pub.

// src/main.rs
mod utils; // Declares the module from src/utils.rs

fn main() {
    // This works if utils.rs exports the function as pub
    utils::helper(); 
}

If you are using a workspace, ensure the crate you are referencing is listed in the workspace members and that you are building the correct target. Sometimes, if you are in a sub-crate, you need to use the crate name as a prefix (e.g., my_workspace_crate::module) rather than just module. Finally, if you recently added a file, run cargo clean followed by cargo build to force a full re-index of your source tree, as the compiler sometimes caches module discovery results.