Sub topic 5
Fixing unresolved names and imports
This chapter assumes you can read the parts of a rustc diagnostic (error code, message, span, help) and run cargo check. It covers errors where the compiler cannot find a name you used.
What "cannot find" means
Rust resolves every name before it checks types. If a name is not in scope, rustc stops with an error from the E04xx family. The name may be misspelled, defined in another module, or not imported. The compiler's message tells you which kind of name it could not find: value, type, function, or module.
| Code | Message starts with | Typical cause |
|---|---|---|
| E0425 | cannot find value / function in this scope | Typo, or the item is in another module |
| E0412 | cannot find type in this scope | Missing `use` for a type |
| E0432 | unresolved import | Wrong path in a `use` line |
| E0433 | failed to resolve: use of undeclared type or module | Path starts with a name that is not in scope |
Case 1: a typo
fn main() {
let count = 3;
println!("{}", cnt);
}rustc reports E0425, cannot find value 'cnt' in this scope. It usually adds a help line such as "a local variable with a similar name exists: count". Rustc compares the unknown name with names in scope and suggests the closest one.
Use the name that is defined:
fn main() {
let count = 3;
println!("{}", count);
}Before you accept a suggestion, check that it is the name you meant. Similar names can belong to different things.
Case 2: a missing import
Standard library types outside the prelude need a use line. The prelude is the small set of names Rust brings into every file, such as Vec, String, and Option. HashMap is not in it.
fn main() {
let mut scores = HashMap::new();
scores.insert("Ana", 10);
println!("{:?}", scores);
}This fails with E0433, failed to resolve: use of undeclared type 'HashMap'. The help text suggests use std::collections::HashMap;. Add it at the top of the file:
use std::collections::HashMap;
fn main() {
let mut scores = HashMap::new();
scores.insert("Ana", 10);
println!("{:?}", scores);
}Case 3: a wrong path or a private item
An item in a module is private by default. A use line can fail either because the path is wrong or because the item is not pub.
mod shapes {
pub fn area(w: u32, h: u32) -> u32 {
w * h
}
}
use shapes::areas;
fn main() {
println!("{}", areas(2, 3));
}rustc reports E0432, unresolved import 'shapes::areas', and notes that there is a similarly named function area. If you fix the spelling but leave out pub, you get a different error, E0603, function 'area' is private. Read the code first, because the fix differs. A wrong path needs a corrected name. A private item needs pub, but only if you want it to be part of the module's public interface.
mod shapes {
pub fn area(w: u32, h: u32) -> u32 {
w * h
}
}
use shapes::area;
fn main() {
println!("{}", area(2, 3));
}Paths in a project
When a name comes from your own crate, anchor the path. crate:: starts at the crate root, self:: starts at the current module, and super:: starts at the parent module. Many E0432 errors come from a relative path that starts in the wrong place.
External crates add one more cause. If you use rand::random and see E0432 or E0433 about rand, the crate is not listed in Cargo.toml. Run cargo add rand, or edit the [dependencies] section, then run cargo check again.
A checklist
- Compare the name in the span with the definition, letter by letter.
- Check whether the item lives in another module, and whether it is
pub. - Add or correct the
useline, anchoring it withcrate::when unsure. - For external crates, confirm the dependency in
Cargo.toml. - Run
cargo checkagain.
Exercise
The line let v: Vec<u8> = Vec::new(); let m: HashMap<u8, u8> = HashMap::new(); fails with E0412 on the type annotation. The code never mentions Vec in an error. Why does Vec resolve while HashMap does not, and what single line fixes it?
Show answer
Vec is in the prelude, which Rust imports into every file, so it resolves without a use. HashMap is not in the prelude. Add use std::collections::HashMap; at the top of the file.
Exercise
You write use utils::helper; for a function declared as fn helper() in mod utils. rustc reports E0603. What does it mean, and how do you fix it?
Show answer
E0603 means the item is private. The path is correct, but the function is not visible outside its module. Change the declaration to pub fn helper() if it should be part of the module's public interface.