/commands/rust/rustc-explain-error-code
rustc --explain E0382: show the explanation of a Rust error code
- tool
- Rust
- versions
- Rust 1.0+
- os
- any
rustc --explain E0382 prints the long explanation of a Rust compiler error code: what the error means, a minimal example that triggers it, and how to fix it. The code is the one rustc shows in brackets, as in error[E0382]: borrow of moved value. The leading E is optional, and the same text is published online in the Rust error codes index.
Explain an error from your build
cat > moved.rs <<'EOF'
fn main() {
let names = vec![String::from("ada")];
let taken = names;
println!("{} {}", names.len(), taken.len());
}
EOF
rustc --edition 2024 moved.rsOutput · exit 1
error[E0382]: borrow of moved value: `names`
--> moved.rs:4:23
|
2 | let names = vec![String::from("ada")];
| ----- move occurs because `names` has type `Vec<String>`, which does not implement the `Copy` trait
3 | let taken = names;
| ----- value moved here
4 | println!("{} {}", names.len(), taken.len());
| ^^^^^ value borrowed here after move
|
help: consider cloning the value if the performance cost is acceptable
|
3 | let taken = names.clone();
| ++++++++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0382`.The last line of rustc's output suggests the command itself: For more information about this error, try rustc --explain E0382.
rustc --explain E0382 | grep -v '^```' | head -n 14Output
A variable was used after its contents have been moved elsewhere.
Erroneous code example:
struct MyStruct { s: u32 }
fn main() {
let mut x = MyStruct{ s: 5u32 };
let y = x;
x.s = 6;
println!("{}", x.s);
}
Since `MyStruct` is a type that is not marked `Copy`, the data gets moved outThe explanation is written in Markdown; the grep -v only drops the code-fence lines from this excerpt. The full text continues with more examples and fixes (cloning, borrowing with &, reference counting with Rc). In a terminal, rustc pipes it through a pager such as less; press q to quit.
Usage
| Command | What it does |
|---|---|
| rustc --explain E0382 | Print the explanation for error code E0382 |
| rustc --explain 0382 | Same; the E prefix is optional |
| cargo --explain E0382 | Same text through Cargo, using the toolchain of the project |
| rustc --explain E0382 | less | Page through it manually, also where no pager is started automatically |
| rustc -W help | List lints (named warnings such as unused_variables) with their default levels |
Reading the output
Each explanation starts with a one-line summary, then an "Erroneous code example" in a code block, the reason the compiler rejects it, and one or more corrected versions. The examples are tested with the compiler, so they compile (or fail) exactly as shown for your toolchain version. For the errors people meet most often, errcodex has pages with reproductions and verified fixes: E0382 borrow of moved value, E0502 cannot borrow as mutable, E0308 mismatched types and E0106 missing lifetime specifier.
Pitfalls
- Unknown codes. A code that does not exist, or a typo, prints
error: E9999 is not a valid error code(with your code) and exits with status 1. - Removed codes. Some old codes are no longer emitted; their explanation stays in the index and is marked as such.
- Lints are not codes.
warning: unused variablehas noEnumber; its name (unused_variables) is shown in a#[warn(...)]note under the warning. - IDE messages from rust-analyzer show the same codes, so the command works for errors seen only in the editor too.
Confirm it worked
The command prints the explanation and exits with status 0. If you see a usage message instead, check that the code follows --explain directly.
FAQ
Does rustc --explain need an internet connection?
No. The explanations are compiled into rustc, so the command works offline and matches the version of the compiler you have installed.
Can I use cargo instead of rustc to explain an error?
Yes. cargo --explain E0382 runs rustc --explain with the project's toolchain, which is the same text.
Why are warnings not listed in the error index?
Warnings come from lints, which are named (unused_variables, dead_code) instead of numbered. Look them up in the rustc lint listing, or run rustc -W help to print every lint with its default level.