Sub topic 13
Cascading errors: fix the first one first
This chapter assumes you can read a rustc diagnostic (error code, message, labels, help) and run cargo check. Here you will learn why one mistake can produce a long list of errors, and why the first error is usually the one to fix.
Why one mistake becomes many errors
The compiler does not stop at the first problem. It keeps going to report as much as it can. But once it hits a name or type it cannot understand, later code that depends on that name can fail too. Those later errors describe the fallout of the first problem and are not separate bugs.
Consider this program:
fn origin() -> Point {
Point { x: 0, y: 0 }
}
fn describe(p: &Point) -> String {
format!("({}, {})", p.x, p.y)
}
fn main() {
println!("{}", describe(&origin()));
}cargo check reports several errors. Typically these are E0412 (cannot find type Point) in each signature and E0422 (cannot find struct Point) at the struct literal. The code has one root cause: Point is never defined or imported. Adding the definition clears every one of those errors at once:
struct Point {
x: i32,
y: i32,
}
fn origin() -> Point {
Point { x: 0, y: 0 }
}
fn describe(p: &Point) -> String {
format!("({}, {})", p.x, p.y)
}
fn main() {
println!("{}", describe(&origin()));
}Fix in order
Follow this routine:
- Run
cargo checkand scroll to the first error. The terminal shows the last errors at the bottom, but the first one is at the top of the output. - Fix only that error, or only the errors that share its cause.
- Run
cargo checkagain. - Repeat until the output is clean.
Re-running after each fix is cheap, because cargo check skips code generation. A fix often removes many errors, so fixing several unrelated ones in one pass makes it hard to tell which edit helped.
Errors that appear in waves
The compiler works in phases. Name resolution and type checking come first. Borrow checking runs only on code that type-checks. So after you fix all type errors, new errors such as E0382 (use of moved value) or E0502 (conflicting borrows) can appear. This is normal. The compiler was not hiding bugs from you. It could not analyze ownership in code it did not yet understand.
Expect this pattern: a long list shrinks to a short one, then a different kind of error shows up.
Reading the final line
The last line of the output is a summary such as error: could not compile ... due to 3 previous errors. Use it as a progress counter. If the count drops after your fix, you are on track. If it rises, your fix may have introduced a new mistake, or it let the compiler reach a later phase and find more.
When to break the rule
Fixing the first error first is a strong default, not a law. Some errors are independent. A typo in one function and a missing mut in another do not affect each other. If several errors point at unrelated code and each has a clear help: message, you can fix them together. Warnings can also wait until the errors are gone.
If a later error makes no sense after you fixed the first, read the later one again. It may have been real all along.
Exercise
cargo check prints 12 errors. The first is E0433 (failed to resolve: use of undeclared type HashMap), and the other 11 mention HashMap in later lines. What should you do first, and what should you expect after the fix?
Show answer
Fix only the first error by adding use std::collections::HashMap;, then run cargo check again. Most or all of the 11 related errors should disappear. A few new errors, such as ownership or borrow errors, may appear because the compiler can now analyze code it could not understand before.