Sub topic 2
The compile-fix loop with cargo check
This chapter assumes you can run cargo build and cargo run, and that you can read the basic parts of a rustc diagnostic: the error code, the message, and the source labels. You will use cargo check to build a fast, repeatable loop for fixing errors.
Why use cargo check
cargo build type-checks and borrow-checks your code, then generates machine code and links a binary. The last two steps are the slow ones. cargo check runs every analysis that can produce an error and then stops. It writes no binary, so it finishes sooner. Any error that cargo build reports about types, names, ownership, or lifetimes also appears in cargo check.
cargo checkA clean run prints a line like Finished dev profile and exits with status 0. A failing run prints diagnostics and exits with a non-zero status. A few errors only appear during code generation or linking. These are rare, so run cargo build before you ship.
The loop
Use the same five steps every time:
- Run
cargo check. - Read the first error in full, from the
error[...]line to the lasthelpline. - Make one change that addresses it.
- Run
cargo checkagain. - Repeat until the output is clean, then run
cargo runorcargo test.
A worked pass
This function fails to compile because of a trailing semicolon.
fn double(x: i32) -> i32 {
x * 2;
}
fn main() {
println!("{}", double(4));
}cargo check reports error[E0308]: mismatched types. The label on the function signature says the function should return i32 but returns (). The help line suggests removing the semicolon. A statement ends in a semicolon and has no value, so the block produces (). Removing the semicolon makes x * 2 the final expression, which is the return value.
fn double(x: i32) -> i32 {
x * 2
}
fn main() {
println!("{}", double(4));
}Run cargo check again. The error is gone and the output is clean. This is one full pass of the loop.
Habits that keep the loop fast
- Fix one error at a time, then re-check. A single change can clear several later errors, as the cascading errors chapter explains.
- Read the whole diagnostic before editing. The
noteandhelplines often name the exact fix. - Keep each change small. If a check shows new errors, you know which edit caused them.
- Look at the top of the output first. In a long list, the earliest error is the one to fix.
Reading the output efficiently
Long output scrolls past the top of your terminal. Two options help:
cargo check 2>&1 | lesslets you scroll from the first error downward.cargo check --message-format shortprints one line per diagnostic, which gives you an overview before you read the details.
When the loop gets stuck
If you change code and the same error returns, check three things:
- You edited the file that the diagnostic points to, and you saved it.
- The fix addresses the primary span, not only a label that points elsewhere.
- The error code is the same as before. A different code means you fixed one problem and exposed another.
For an unfamiliar code, run rustc --explain E0308 to read its long description. The next chapter covers this in detail.
Exercise
You run cargo check and it reports 12 errors. What do you do first, and why do you re-run cargo check after each change instead of fixing all 12 at once?
Show answer
Read and fix the first error. Later errors are often side effects of earlier ones, so one fix can clear several. Re-running after each change also shows which edit caused any new error.
Exercise
A program passes cargo check but you still cannot run it. Which command do you use next, and why was cargo check fast?
Show answer
Use cargo run, or cargo build if you only need the binary. cargo check was fast because it skips code generation and linking, so it writes no binary.