Sub topic 3
Error codes and rustc --explain
This chapter assumes you can run cargo check, read the basic layout of a rustc diagnostic (message, span, labels, help), and know what a move and a borrow are. Here you learn what the code in brackets means and how to get more detail on it.
What an error code is
Most rustc errors start with a code in square brackets, such as error[E0382]. The code is a stable identifier for one category of error. The message text can change between compiler versions, and the same text can be worded differently depending on the types involved. The code stays the same.
This makes codes useful in three ways:
- You can search for
E0382and find relevant results, which is harder with a message that contains your own variable names. - You can compare errors across projects and compiler versions.
- You can ask the compiler for a full explanation of the category.
Not every diagnostic has a code. Some errors, such as certain syntax errors, and all warnings from lints like unused_variables, carry a lint name or no identifier at all. Treat the code as a handle when one exists.
Asking the compiler to explain
The rustc --explain command prints a long description of an error code. It works offline, because the text ships with the compiler.
cargo check
rustc --explain E0382The first command reports errors. The second prints the explanation for E0382, which covers use of a moved value.
An explanation usually has four parts:
- A short statement of what the error means.
- A small example that triggers it, marked as erroneous.
- A description of why the compiler rejects it.
- One or more corrected examples.
The cargo output also hints at this. At the end of a failed build you will often see a line like For more information about this error, try rustc --explain E0382.
A worked example
Take this program, which moves a String and then uses the original.
fn main() {
let name = String::from("Ferris");
let greeting = name;
println!("{name} {greeting}");
}cargo check reports error[E0382]: borrow of moved value: name. The label says the value was moved on the line let greeting = name;, and a note says String does not implement the Copy trait.
Now run rustc --explain E0382. The explanation describes the same pattern with a different type and lists the usual fixes: borrow instead of move, clone, or restructure so the original is no longer needed. You can map those options to your own code. Here, name.clone() keeps both bindings valid.
fn main() {
let name = String::from("Ferris");
let greeting = name.clone();
println!("{name} {greeting}");
}Where the code fits in the loop
You do not need to explain every error. Use --explain when the message and help text do not tell you why the rule exists, or when the suggested fix does not match what you intend.
Common mistakes
- Running
rustc --explainwith a lint name such asunused_variables. It accepts error codes only. Lints are documented in the rustc book. - Copying the example fix without checking it. The explanation shows a minimal case. Your code may need a different option, such as borrowing instead of cloning.
- Passing the code without the
E. WriteE0382, not0382.
Quick reference
| Code | Meaning | Chapter topic |
|---|---|---|
| E0308 | Mismatched types | Type mismatch |
| E0382 | Use of moved value | Ownership moves |
| E0502 | Conflicting borrows | Borrow conflicts |
| E0106 | Missing lifetime specifier | Lifetimes |
| E0277 | Unsatisfied trait bound | Trait bounds |
| E0004 | Non-exhaustive patterns | Exhaustive match |
Exercise
Your build fails with error[E0502]. Name the command that prints the full explanation, and give one reason to read it when the diagnostic already has a help message.
Show answer
Run rustc --explain E0502. The help message only suggests a fix for your specific code. The explanation describes the rule behind the error and shows several corrected examples, so you can choose the fix that fits your intent instead of the first one suggested.
Exercise
Why is searching for an error code usually better than searching for the full error message?
Show answer
The message contains names from your own code, such as variable and type names, so it rarely matches other people's posts. The code is the same for everyone who hits that category of error.