Rust Common Error Codes
An error code is the compiler's short name for one kind of failure. Each code keeps one meaning forever.
E0308 — the type does not match
You promised one type and handed over another. Convert the value (parse, cast, or build it properly) — never just wish.
let n: i32 = "42";E0382 — the value already moved
A non-Copy value can live in only one place. Borrow it with & when the new owner only needs a look.
let name = String::from("Ferris");
let moved = name;
println!("{}", name);E0502 — a reader and a writer clash
While a borrow is alive, the owner cannot change the value. Finish reading first, then write — or copy the small value out.
let first = &v[0];
v.push(4);E0499 — two writers at once
Only one mutable borrow may exist at a time. Use the first, let it end, then take the second.
let a = &mut n;
let b = &mut n;E0597 — the value dies too soon
A reference must never outlive its value. Keep the value in a variable that lives at least as long as the reference — or hand over ownership instead.
let r;
{
let x = 5;
r = &x;
}
println!("{}", r);E0277 — a needed trait is missing
The compiler names the type and the trait it needed. Usually the fix is one derive line, or implementing the trait yourself.
struct Point { x: i32, y: i32 }
println!("{:?}", p);Example
#[derive(Debug)]
struct Point { x: i32, y: i32 }
fn main() {
let score: i32 = "42".parse().unwrap();
let name = String::from("Ferris");
let greeting = &name;
let mut v = vec![1, 2, 3];
let first = v[0];
v.push(4);
let mut n = 10;
let a = &mut n;
*a += 1;
let b = &mut n;
*b += 1;
let owned = String::from("mine");
let p = Point { x: 1, y: 2 };
println!("score = {}", score);
println!("{} says hi, first = {}, v = {:?}", greeting, first, v);
println!("n = {}, owned = {}, p = {:?}", n, owned, p);
}Key points
- The six in this lesson cover most everyday stops.
- They are a wrong type, a moved value, two borrow clashes, a too-short lifetime, and a missing trait.
- The fix is almost always a shape change.
- Borrow instead of move.
- Finish one borrow before the next.
- Or convert the value.
- clone() ends most move errors when a copy is acceptable — reach for it when borrowing gets tangled
- Each code keeps one meaning forever, so anything you learn about E0502 today still applies in ten years
- Each code names its problem and points at the shape of the fix.
- An error code gives one kind of compiler failure a short, fixed name.
In this example, all six fixes from this lesson run in one program. The score is parsed, fixing E0308. name is borrowed, not moved, fixing E0382. The first element is copied out before the push, fixing E0502. The two mutable borrows of n happen one after the other, fixing E0499. owned is a value, not a reference to a dying temporary, fixing E0597. And Point derives Debug, fixing E0277. The output shows every value exactly as the fixed code promises.
Note
You will meet E0277 far beyond Debug.
Iterators, threads and async all report missing traits in this same shape.
The shape names the type, the trait, and where the trait was needed.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Rust Playground
Exercise
You get E0502 while pushing to a Vec. What is the usual fix?