Skip to content
Learn Rust
Theme

Reading compiler errors

Rust's errors look long, but they follow a fixed layout and they are unusually helpful. Learning to read them is the fastest way to get better at Rust. This page shows the layout, then lists every error code that appears on this site.

How an error is laid out

This is a real error, from Vectors and Slices.

$ cargo buildWhat is E0507?
error[E0507]: cannot move out of index of `Vec<String>`
 --> src/main.rs:3:17
  |
3 |     let first = names[0];
  |                 ^^^^^^^^ move occurs because value has type `String`, which does not implement the `Copy` trait
  |
help: consider borrowing here
  |
3 |     let first = &names[0];
  |                 +
help: consider cloning the value if the performance cost is acceptable
  |
3 |     let first = names[0].clone();
  |                         ++++++++
  1. The error[E0507] line names the rule that was broken. Look up the code with rustc --explain E0507, or find it in the index below.
  2. --> gives the file, line and column.
  3. The - marks are the other parts of the story: here, where the borrow started and where it is used later.
  4. The ^ marks where the compiler gave up. Read the marked lines in order, top to bottom.
  5. help: is a suggestion. It is often right. When it suggests .clone(), check first: there is usually a fix that copies nothing.

What to do first

  1. Read the first error only. Later errors are often caused by the first one. Fix it and build again.
  2. Read the message, then the marked lines, then help:. The message says what rule was broken; the marks show where; the help often shows a fix.
  3. Check the types. Many errors come down to "this is a &String, not a String". Your editor can show the type of any variable (rust-analyzer's inlay hints).
  4. Run rustc --explain E0502 (with your code) for a longer explanation with examples, or open https://doc.rust-lang.org/error_codes/E0502.html.
  5. If the fix suggested is .clone(), ask whether you can borrow instead. Cloning is fine, but it should be a choice.

Every error code on this site (80)

Each entry shows a real message from a real run on this site, a plain explanation, and every page where you can see it happen.

E0004 non-exhaustive patterns: `&Shape::Triangle(_, _)` not covered

What it means. A match does not cover every possible value, so the compiler cannot promise one of the arms will run.

What people usually do. Add the missing arms, or a final _ => arm for "everything else". Naming every case is safer: when you add a case later, the compiler tells you where to handle it.

E0005 refutable pattern in local binding

What it means. A let pattern can fail for some values (for example let Some(x) = opt;), and a plain let has nowhere to go when it does.

What people usually do. Use let ... else { return/continue/panic } to say what happens on failure, or if let / match to handle both cases.

E0015 cannot call non-const function `helper` in constant functions

What it means. A const or static is worked out at compile time, and you called a function that can only run at run time (such as String::from). Only const fn functions can be called there.

What people usually do. Use a type and value that exist at compile time (a &str or a number), or build the value at run time inside main with let.

E0027 pattern does not mention fields `y`, `z`

What it means. A struct pattern has to account for every field. This one leaves some out without saying so.

What people usually do. Name the missing fields, or end the pattern with .. to say you do not care about the rest: Point { x, .. }.

E0034 multiple applicable items in scope

What it means. Two traits in scope both give this type a method with the same name, so the call is ambiguous. Having two such traits is fine; only calling the bare name fails.

What people usually do. Say which one you mean: TraitA::name(&value) or <Type as TraitA>::name(&value).

E0038 the trait `Source` is not dyn compatible

What it means. You used dyn Trait, but the trait is not dyn compatible: a method returns Self, takes a generic parameter, or the trait requires Sized or Clone. A trait object cannot call those through a vtable.

What people usually do. Take the offending method off the trait, add where Self: Sized to it, or use generics (T: Trait) instead of dyn Trait.

E0046 not all trait items implemented, missing: `volume`

What it means. An impl Trait for Type block is missing a method (or associated type or constant) that the trait requires and gives no default for.

What people usually do. Add the missing item the message names, or give it a default in the trait.

E0061 this function takes 1 argument but 3 arguments were supplied

What it means. You called a function with the wrong number of arguments. Rust has no default or optional arguments and no overloading, so the count always has to match the definition.

What people usually do. Pass every argument, or change the function. For "optional" values use Option<T> parameters, a small config struct, or a builder.

E0063 missing fields `active` and `age` in initializer of `User`

What it means. You built a struct and left out a field. Every field needs a value.

What people usually do. Add the field. If the type has a sensible default, ..Default::default() fills the rest.

E0072 recursive type `Expr` has infinite size

What it means. A type contains itself with no pointer in between, so its size would be infinite. This happens with recursive enums and structs such as an expression tree.

What people usually do. Put the recursive part behind a pointer: Box<Expr> instead of Expr. The Box lives on the heap and has a fixed size.

E0080 constructing invalid value of type bool: encountered 0x03, but expected a boolean

What it means. The compiler runs a const or static initialiser while it compiles, and that evaluation failed: it hit undefined behaviour, overflowed, indexed out of bounds or otherwise panicked.

What people usually do. Read the first line: it says what the evaluation did wrong. Fix the constant so the computation is valid, or make it a run-time value in main if it cannot be computed up front.

E0106 missing lifetime specifier

What it means. A function returns a reference, and the compiler cannot tell which argument it borrows from.

What people usually do. Add a lifetime name so the signature says which argument the result borrows from, or return an owned value (String, Vec) instead.

E0107 function takes 0 generic arguments but 1 generic argument was supplied

What it means. You gave the wrong number of generic arguments: too many, too few, or a turbofish on something that has none (for example on a function that takes impl Trait).

What people usually do. Remove the extra arguments, or change the signature to a named type parameter (fn f<T: Trait>(x: T)) if callers need to write the type.

E0117 only traits defined in the current crate can be implemented for types defined outside of the crate

What it means. The orphan rule: you can implement a trait for a type only if the trait or the type is defined in your own crate. Here both come from elsewhere.

What people usually do. Wrap the foreign type in your own struct (a newtype) and implement the trait for that, or define your own trait.

E0119 conflicting implementations of trait `Describe` for type `i32`

What it means. Two impl blocks could both apply to the same type, and Rust has no specialization to pick one. A blanket impl and a specific impl for the same trait overlap in this way.

What people usually do. Remove one impl, narrow the blanket impl with a bound that excludes the specific case, or use a default method in the trait for the fallback.

E0133 call to unsafe function `set_var` is unsafe and requires unsafe block

What it means. You used something that needs an unsafe block (a raw pointer dereference, a call to an unsafe function, a static mut) outside of one.

What people usually do. Most programs never need it. Look for a safe alternative first; if you really need it, wrap the smallest possible piece in unsafe { ... } and say why it is sound.

E0170 pattern binding `North` is named the same as one of the variants of the type `Direction`

What it means. In a match, a bare name like North is a new variable that matches anything, not the enum variant. It is named the same as a variant, which is almost always a typo. This one is a deny-by-default lint.

What people usually do. Write the path: Direction::North, or use Direction::*; if you want bare names.

E0184 the trait `Copy` cannot be implemented for this type; the type has a destructor

What it means. A type that implements Drop cannot also be Copy. A bit-for-bit copy would run the destructor twice.

What people usually do. Drop the Copy derive and use Clone where you need a copy, or remove the Drop impl.

E0195 lifetime parameters or bounds on method `say_hello` do not match the trait declaration

What it means. A method in your impl has lifetime parameters or bounds that do not match the one the trait declares. The two signatures must agree, including lifetimes, even when both look like "just a reference".

What people usually do. Match the trait exactly. If the trait is generated with an attribute macro, put the same macro on your impl: for tonic that is #[tonic::async_trait] (a different async_trait gives the same error again).

E0204 the trait `Copy` cannot be implemented for this type

What it means. You asked for Copy on a type that has a field that cannot be copied bit for bit, such as a String or a Vec.

What people usually do. Drop Copy and use Clone when you want a copy, or change the field to something that is Copy.

E0277 `{async fn body of f()}` cannot be unpinned

What it means. A type does not implement a trait that the code needs. This is the most common error in Rust and the message varies a lot: cannot multiply an i32 by an f64, cannot print with {}, cannot use ? here, cannot send between threads.

What people usually do. Read the line that starts with the trait ... is not implemented for. Then either convert the value (as f64), derive or implement the trait, or change the type.

E0282 type annotations needed for `Vec<_>`

What it means. The compiler cannot work out a type from the code around it, so it asks you to say. Typical causes: Vec::new() or HashMap::new() that nothing has used yet, and parse() or collect() without a target type.

What people usually do. Write the type where it is first created: let mut m: HashMap<String, Vec<u32>> = HashMap::new();, or use the turbofish: .collect::<Vec<_>>().

E0283 type annotations needed

What it means. The compiler cannot work out which type you mean, because more than one would fit. Functions like sum() and collect() can produce many types.

What people usually do. Say which one: let total: i32 = ...sum(); or .collect::<Vec<_>>().

E0284 type annotations needed

What it means. Like E0282, the compiler cannot decide a type, here because a method such as parse() can produce many types and nothing says which. The message mentions a trait (FromStr) because it needs a type that implements it.

What people usually do. Name the type you want: let n: i32 = "42".parse().unwrap(); or "42".parse::<i32>().

E0308 mismatched types

What it means. The compiler expected one type and found another. Typical causes: two if branches that give different types, a function body that ends with a semicolon, or an Option<T> where a T is wanted.

What people usually do. Make the two sides agree: convert one, or unwrap/handle the Option. If a function returns nothing, check for a stray ; on its last line.

E0317 `if` may be missing an `else` clause

What it means. An if used as a value has no else, so when the condition is false it would produce () (nothing), which is not the type of the other branch.

What people usually do. Add an else that gives a value of the same type, or use if as a statement and put the value elsewhere.

E0368 binary assignment operation `+=` cannot be applied to type `&mut {integer}`

What it means. A compound assignment such as += was used on a type that does not support it. A common cause is x += 1 where x is a reference (&i32) from a loop: the loop gave you a borrowed value, not the number itself.

What people usually do. Dereference first (*x += 1) when the loop gives &mut i32, or loop over &mut v instead of &v. If the loop gave a copy of a plain value, changing the copy never changes the list.

E0369 no implementation for `&HashSet<Tag> & &HashSet<Tag>`

What it means. An operator such as %, + or == is not defined for the types you gave it. A common cause is a reference to a reference, which arithmetic does not see through.

What people usually do. Take the references off first: *n, **n, or a pattern like |&&n|.

E0373 async block may outlive the current function, but it borrows `s`, which is owned by the current function

What it means. A closure borrows a local variable, but the closure may run after the function that owns that variable has finished. This is what happens with thread::spawn.

What people usually do. Add move so the closure takes ownership of what it uses.

E0381 used binding `count` isn't initialized

What it means. A variable was declared but might be read before it has any value, on at least one path through the code.

What people usually do. Give it a value where it is declared, or make sure every branch assigns it before it is used.

E0382 borrow of moved value: `scores`

What it means. A value was moved somewhere else (assigned, passed to a function, put into a collection), and you used the old name afterwards.

What people usually do. Borrow instead of moving (&value), clone it if you really need two (.clone()), or restructure so the last use comes after the move.

E0384 cannot assign twice to immutable variable `i`

What it means. You assigned to a variable twice, but variables are immutable unless you say otherwise.

What people usually do. Declare it let mut.

E0392 type parameter `State` is never used

What it means. A generic parameter (a type or a lifetime) is declared on a struct or enum but never used in any field, so the compiler cannot tell what it is for.

What people usually do. Use it in a field, or, if it only marks a type for the compiler, store a PhantomData<T> field.

E0407 method `perimiter` is not a member of trait `Shape`

What it means. You wrote a method inside an impl Trait for Type block that the trait does not have. Usually a typo in the method name, because there is no override keyword to catch it.

What people usually do. Check the trait definition for the exact method name and signature. The help: line often names the closest match.

E0408 variable `a` is not bound in all patterns

What it means. In an or-pattern (A(x) | B(y)), every alternative has to bind the same names. One side binds a name the other does not.

What people usually do. Bind the same name in every alternative, or split the alternatives into separate arms.

E0422 cannot find struct, variant or union type `switch` in this scope

What it means. You used a name as a struct (a literal like Foo { .. }) but no struct, enum variant or type alias with that name is in scope. A common cause is a typo, a missing use, or trying to write a class or object literal from another language.

What people usually do. Check the spelling and add the use. Rust builds values with a struct declared by struct Name { ... } and written Name { field: value }.

E0423 cannot find function `println` in this scope

What it means. You used a name where the compiler expected something else. A common case: calling a macro like a function (println("hi") instead of println!("hi")), or writing a struct name where a value is expected.

What people usually do. Add the ! for macros, or check the name: the help: line says which one it found.

E0424 cannot find value `self` in this scope

What it means. You used self in a function that does not take self as its first parameter. Methods have to say they take self, &self or &mut self; there is no implicit this.

What people usually do. Add &self (or &mut self, or self) as the first parameter, or call the function as an associated function without self.

E0425 cannot find function `factorial` in this scope

What it means. The compiler cannot find a name: a variable, function or constant that is not in scope. Common causes are a typo, a variable declared inside a block you have left, or a missing use.

What people usually do. Check the spelling and where the name is declared. The help: line often suggests a similar name that does exist.

E0428 the name `show` is defined multiple times

What it means. The same name is defined twice in one scope, for example two structs or two functions with the same name.

What people usually do. Rename one. Rust does not allow function overloading, so two functions cannot share a name even with different parameters.

E0433 cannot find type `HashMap` in this scope

What it means. A name could not be found: a module, a crate or a type is used but never brought into scope.

What people usually do. Add the use line, declare the module with mod, or add the crate to Cargo.toml.

E0434 can't capture dynamic environment in a fn item

What it means. A function declared with fn inside another function cannot use the outer function's local variables. Only a closure (|x| ...) can capture them.

What people usually do. Pass the value in as a parameter, or write the inner function as a closure.

E0435 attempt to use a non-constant value in a constant

What it means. An array length (or another compile-time value) was given a normal variable. It must be a constant the compiler knows before the program runs.

What people usually do. Use a const, a literal, or switch to a Vec, whose length can be decided at run time.

E0451 field `balance_cents` of struct `Account` is private

What it means. You built a struct with a literal, but one of its fields is private to the module that defines it, so code outside cannot set it.

What people usually do. Make the field pub, or give the module a function such as Item::new(...) that builds the value for outsiders.

E0499 cannot borrow `list` as mutable more than once at a time

What it means. Two mutable borrows of the same value are alive at the same time. Rust allows one writer at a time.

What people usually do. Finish with the first borrow before taking the second, or split the data so each borrow touches a different part.

E0500 closure requires unique access to `*self` but it is already borrowed

What it means. A closure needs to borrow something mutably (here all of self) while another borrow of it, such as an iterator over one of its fields, is still in use.

What people usually do. Borrow only the fields the closure needs, copy what you need out first, collect before you mutate, or use a plain for loop and call the method after the borrow ends.

E0502 cannot borrow `*self` as mutable because it is also borrowed as immutable

What it means. You are changing a value while something else is still reading it (or the other way round). A reader and a writer cannot overlap.

What people usually do. End the read first (copy out what you need), change in a separate step, or use an index instead of holding a reference.

E0503 cannot use `n` because it was mutably borrowed

What it means. You used a value (read or copied it) while it is mutably borrowed, and the borrow is used again later.

What people usually do. Finish with the &mut first, or read the value through the reference (*r) instead of the owner.

E0505 cannot move out of `msg` because it is borrowed

What it means. You moved a value (or tried to) while a reference to it is still going to be used later.

What people usually do. Finish using the reference before the move, or borrow the value instead of moving it. Clone only if you really need two.

E0506 cannot assign to `position` because it is borrowed

What it means. You assigned a new value to something that is still borrowed. A reference that is used afterwards would suddenly see a different value.

What people usually do. Finish using the reference first, or take the new value after the borrow ends.

E0507 cannot move out of `name`, a captured variable in an `FnMut` closure

What it means. You tried to take a value out of something you only borrow, such as an item of a Vec by index. That would leave a hole behind.

What people usually do. Clone it, borrow it, or use a method that removes it properly: remove, swap_remove, std::mem::take, Option::take.

E0508 cannot move out of type `[T]`, a non-copy slice

What it means. You tried to move a value out of an array or slice by indexing it (list[0]). The element type is not Copy, and an index cannot leave a hole behind in the slice.

What people usually do. Borrow the element (&list[0]), clone it (list[0].clone()), or, for a generic T, add a Copy or Clone bound.

E0509 cannot move out of type `Session`, which implements the `Drop` trait

What it means. You tried to move a field out of a type that has its own Drop. Its drop would then see a half-empty value.

What people usually do. Clone the field, borrow it, or use std::mem::take to leave something valid behind.

E0515 cannot return reference to local variable `s`

What it means. A function tries to return a reference to something it created itself. That something is freed when the function ends, so the reference would dangle.

What people usually do. Return the owned value (String, not &str), or take a reference as a parameter and return a part of that.

E0527 pattern requires 2 elements but array has 3

What it means. A slice pattern for an array has a different number of elements than the array. For arrays the length is part of the type, so the compiler checks it.

What people usually do. Match the length exactly, or add .. to cover the rest: [first, ..]. Use a slice (&v[..]) to match any length.

E0529 expected an array or slice, found `Vec<{integer}>`

What it means. A slice pattern like [first, rest @ ..] only matches an array or a slice, and the value you matched is something else, such as a Vec.

What people usually do. Match on a slice: match v.as_slice() { ... } or match &v[..] { ... }, as the compiler's "consider slicing here" suggests.

E0530 match bindings cannot shadow statics

What it means. A name in a pattern is a new variable, but this name is already a static (or a constant-like item) and a binding cannot shadow it.

What people usually do. Pick a different variable name, or write the path to the item if you meant to compare with it.

E0571 `break` with value from a `for` loop

What it means. break with a value only works in a loop. A for or while loop can end without ever hitting break, so it has no value to give.

What people usually do. Use loop, or an iterator method such as find or position.

E0573 expected type, found variant `Shape::Circle`

What it means. You used an enum variant where a type is expected. Shape::Circle is a value of the type Shape, not a type of its own.

What people usually do. Use the enum as the type (Shape) and match on it, or put the data in a separate struct and have the variant hold that.

E0583 file not found for module `models`

What it means. You declared a module with mod name; but there is no name.rs (or name/mod.rs) for it.

What people usually do. Create the file in the right folder, or use an inline module mod name { ... }.

E0592 duplicate definitions with name `show`

What it means. Two functions with the same name in the same type. Rust has no overloading, so new can exist only once per type.

What people usually do. Give them different names (new and with_balance), or take one Option or a small config struct.

E0594 cannot assign to `count`, as it is a captured variable in a `Fn` closure

What it means. You assigned to something you are not allowed to change: a variable that is not mut, or data behind a shared reference or an index that does not allow it.

What people usually do. Make the binding mut, or use the type's own method (for a HashMap, entry or insert).

E0596 cannot borrow `*view` as mutable, as it is behind a `&` reference

What it means. You called something that needs to change a value (&mut), but the variable is not declared mut, or you only hold a shared reference.

What people usually do. Declare it let mut, or take &mut in the signature.

E0597 `trimmed` does not live long enough

What it means. A reference is used after the value it points at was dropped, usually at the end of an inner block.

What people usually do. Declare the value in the outer scope so it lives longer, or return an owned value instead of a reference to it.

E0599 no method named `len` found for struct `std::sync::mpsc::Receiver<T>` in the current scope

What it means. The method you called does not exist for that type. Often the method is there, but the trait that provides it is not imported.

What people usually do. Check the name. If the error mentions a trait that "is implemented but not in scope", add the use line it suggests.

E0600 cannot apply unary operator `-` to type `u32`

What it means. An operator was applied to a type that does not support it. For example -x or !x on an unsigned integer or a String (there is no negative u32, and ! on a number means bitwise not only for integers).

What people usually do. Use a type the operator supports, or convert first (-(x as i32)). x-- and --x are not decrement in Rust: write x -= 1.

E0603 tuple struct constructor `Port` is private

What it means. You used a name from another module, but that item is private. Everything in Rust is private to its module unless it is marked pub.

What people usually do. Add pub to the item (and to the modules on the path to it) if outside code should use it, or call a public function that does the job.

E0605 non-primitive cast: `i32` as `Level`

What it means. as only converts between primitive types such as integers, floats and char. It cannot turn a number into your own type.

What people usually do. Write a function that matches the number and returns the enum, or implement TryFrom<i32>. Converting an enum into its number with as is allowed.

E0606 casting `char` as `f64` is invalid

What it means. An as cast between these two types is not allowed. as converts between number types, and a few others (u8 to char, a bool or char to an integer), but not every pair: for example char or bool to f64.

What people usually do. Go through a type that is allowed (c as u32 as f64), or use From/TryFrom (f64::from(n)).

E0608 cannot index into a value of type `LinkedList<{integer}>`

What it means. You used [...] on a type that cannot be indexed, such as a string with a number, or an iterator.

What people usually do. Use the right method: .chars().nth(i) or &s[a..b] for strings; .collect::<Vec<_>>() first for iterators.

E0609 no field `name` on type `Option<User>`

What it means. You asked for a field that the type does not have. Sometimes the value is wrapped, such as an Option<User> where you wrote user.name.

What people usually do. Unwrap or match the wrapper first, or check the field name.

E0614 type `{integer}` cannot be dereferenced

What it means. You used * to dereference a value that is not a reference or pointer. The compiler is telling you the value is already the thing itself.

What people usually do. Remove the *. If you expected a reference, check the type: a for x in v loop gives owned items, while for x in &v gives references.

E0615 attempted to take value of method `deposit` on type `Account`

What it means. You wrote x.name without parentheses, but name is a method, not a field. Methods are called with () even when they take no arguments.

What people usually do. Call it: x.name(). If you really want a field, check the struct definition: the name you wrote belongs to a method.

E0616 field `balance` of struct `Account` is private

What it means. You read or wrote a field that is private to its module.

What people usually do. Add a method that exposes what callers need (acct.balance()), or make the field pub if outside code really should touch it directly.

E0658 use of unstable library feature `future_join`

What it means. The code uses a feature that exists in the compiler but has not been stabilised, so it is only available on the nightly toolchain (for example try { } blocks).

What people usually do. Use a stable alternative (a closure that returns a Result, or a helper function and ?), or opt into nightly only if you really need the feature.

E0659 `describe` is ambiguous

What it means. A name is ambiguous because two use globs (use a::*; use b::*;) both bring in an item with that name.

What people usually do. Import the one you want explicitly (use a::describe;), which takes priority over globs.

E0716 temporary value dropped while borrowed

What it means. A temporary value was freed at the end of its statement while a reference to it was still being used afterwards.

What people usually do. Give the value a name with let so it lives as long as the reference does, or keep the whole chain in one statement.

E0728 `await` is only allowed inside `async` functions and blocks

What it means. You used .await in a function that is not async. An await pauses the future it is in, so it needs an async fn or async block around it.

What people usually do. Make the function async (and .await it where it is called), or keep it synchronous and have the runtime call it. This is the "function colour" rule.

E0752 `main` function is not allowed to be `async`

What it means. main cannot be async: the operating system calls it as an ordinary function, and something has to run the future.

What people usually do. Keep fn main() ordinary and start a runtime inside it, or use a runtime's attribute such as #[tokio::main], which writes that wrapper for you.

E0782 expected a type, found a trait

What it means. A bare trait name (Speaker) was used where a type is needed. Since the 2021 edition a trait is not a type, so &Speaker, Vec<Speaker> and -> Speaker are errors.

What people usually do. Use &dyn Speaker or Box<dyn Speaker> for a trait object, impl Speaker or a generic T: Speaker for static dispatch.