Compiler Errors

Rust Compiler Error Index

E0001

Note: this error code is no longer emitted by the compiler.

This error suggests that the expression arm corresponding to the noted pattern will never be reached as for all possible values of the expression being matched, one of the preceding patterns will match.

This means that perhaps some of the preceding patterns are too general, this one is too specific or the ordering is incorrect.

For example, the following match block has too many arms:

match Some(0) {
    Some(bar) => {/* ... */}
    x => {/* ... */} // This handles the `None` case
    _ => {/* ... */} // All possible cases have already been handled
}

match blocks have their patterns matched in order, so, for example, putting a wildcard arm above a more specific arm will make the latter arm irrelevant.

Ensure the ordering of the match arm is correct and remove any superfluous arms.

E0002

Note: this error code is no longer emitted by the compiler.

This error indicates that an empty match expression is invalid because the type it is matching on is non-empty (there exist values of this type). In safe code it is impossible to create an instance of an empty type, so empty match expressions are almost never desired. This error is typically fixed by adding one or more cases to the match expression.

An example of an empty type is enum Empty { }. So, the following will work:

enum Empty {}

fn foo(x: Empty) {
    match x {
        // empty
    }
}

However, this won't:

fn foo(x: Option<String>) {
    match x {
        // empty
    }
}

E0003

Note: this error code is no longer emitted by the compiler.

Not-a-Number (NaN) values cannot be compared for equality and hence can never match the input to a match expression. So, the following will not compile:

const NAN: f32 = 0.0 / 0.0;

let number = 0.1f32;

match number {
    NAN => { /* ... */ },
    _ => {}
}

To match against NaN values, you should instead use the is_nan() method in a guard, like so:

let number = 0.1f32;

match number {
    x if x.is_nan() => { /* ... */ }
    _ => {}
}

E0004

This error indicates that the compiler cannot guarantee a matching pattern for one or more possible inputs to a match expression. Guaranteed matches are required in order to assign values to match expressions, or alternatively, determine the flow of execution. Erroneous code example:

enum Terminator {
    HastaLaVistaBaby,
    TalkToMyHand,
}

let x = Terminator::HastaLaVistaBaby;

match x { // error: non-exhaustive patterns: `HastaLaVistaBaby` not covered
    Terminator::TalkToMyHand => {}
}

If you encounter this error you must alter your patterns so that every possible value of the input type is matched. For types with a small number of variants (like enums) you should probably cover all cases explicitly. Alternatively, the underscore _ wildcard pattern can be added after all other patterns to match "anything else". Example:

enum Terminator {
    HastaLaVistaBaby,
    TalkToMyHand,
}

let x = Terminator::HastaLaVistaBaby;

match x {
    Terminator::TalkToMyHand => {}
    Terminator::HastaLaVistaBaby => {}
}

// or:

match x {
    Terminator::TalkToMyHand => {}
    _ => {}
}

E0005

Patterns used to bind names must be irrefutable, that is, they must guarantee that a name will be extracted in all cases. Erroneous code example:

let x = Some(1);
let Some(y) = x;
// error: refutable pattern in local binding: `None` not covered

If you encounter this error you probably need to use a match or if let to deal with the possibility of failure. Example:

let x = Some(1);

match x {
    Some(y) => {
        // do something
    },
    None => {}
}

// or:

if let Some(y) = x {
    // do something
}

E0007

This error indicates that the bindings in a match arm would require a value to be moved into more than one location, thus violating unique ownership. Code like the following is invalid as it requires the entire Option<String> to be moved into a variable called op_string while simultaneously requiring the inner String to be moved into a variable called s.

let x = Some("s".to_string());

match x {
    op_string @ Some(s) => {}, // error: cannot bind by-move with sub-bindings
    None => {},
}

See also the error E0303.

E0008

Names bound in match arms retain their type in pattern guards. As such, if a name is bound by move in a pattern, it should also be moved to wherever it is referenced in the pattern guard code. Doing so however would prevent the name from being available in the body of the match arm. Consider the following:

match Some("hi".to_string()) {
    Some(s) if s.len() == 0 => {}, // use s.
    _ => {},
}

The variable s has type String, and its use in the guard is as a variable of type String. The guard code effectively executes in a separate scope to the body of the arm, so the value would be moved into this anonymous scope and therefore becomes unavailable in the body of the arm.

The problem above can be solved by using the ref keyword.

match Some("hi".to_string()) {
    Some(ref s) if s.len() == 0 => {},
    _ => {},
}

Though this example seems innocuous and easy to solve, the problem becomes clear when it encounters functions which consume the value:

struct A{}

impl A {
    fn consume(self) -> usize {
        0
    }
}

fn main() {
    let a = Some(A{});
    match a {
        Some(y) if y.consume() > 0 => {}
        _ => {}
    }
}

In this situation, even the ref keyword cannot solve it, since borrowed content cannot be moved. This problem cannot be solved generally. If the value can be cloned, here is a not-so-specific solution:

#[derive(Clone)]
struct A{}

impl A {
    fn consume(self) -> usize {
        0
    }
}

fn main() {
    let a = Some(A{});
    match a{
        Some(ref y) if y.clone().consume() > 0 => {}
        _ => {}
    }
}

If the value will be consumed in the pattern guard, using its clone will not move its ownership, so the code works.

E0009

In a pattern, all values that don't implement the Copy trait have to be bound the same way. The goal here is to avoid binding simultaneously by-move and by-ref.

This limitation may be removed in a future version of Rust.

Erroneous code example:

struct X { x: (), }

let x = Some((X { x: () }, X { x: () }));
match x {
    Some((y, ref z)) => {}, // error: cannot bind by-move and by-ref in the
                            //        same pattern
    None => panic!()
}

You have two solutions:

Solution #1: Bind the pattern's values the same way.

struct X { x: (), }

let x = Some((X { x: () }, X { x: () }));
match x {
    Some((ref y, ref z)) => {},
    // or Some((y, z)) => {}
    None => panic!()
}

Solution #2: Implement the Copy trait for the X structure.

However, please keep in mind that the first solution should be preferred.

#[derive(Clone, Copy)]
struct X { x: (), }

let x = Some((X { x: () }, X { x: () }));
match x {
    Some((y, ref z)) => {},
    None => panic!()
}

E0020

This error indicates that an attempt was made to divide by zero (or take the remainder of a zero divisor) in a static or constant expression. Erroneous code example:

#[deny(const_err)]

const X: i32 = 42 / 0;
// error: attempt to divide by zero in a constant expression

E0023

A pattern used to match against an enum variant must provide a sub-pattern for each field of the enum variant. This error indicates that a pattern attempted to extract an incorrect number of fields from a variant.

enum Fruit {
    Apple(String, String),
    Pear(u32),
}

Here the Apple variant has two fields, and should be matched against like so:

enum Fruit {
    Apple(String, String),
    Pear(u32),
}

let x = Fruit::Apple(String::new(), String::new());

// Correct.
match x {
    Fruit::Apple(a, b) => {},
    _ => {}
}

Matching with the wrong number of fields has no sensible interpretation:

enum Fruit {
    Apple(String, String),
    Pear(u32),
}

let x = Fruit::Apple(String::new(), String::new());

// Incorrect.
match x {
    Fruit::Apple(a) => {},
    Fruit::Apple(a, b, c) => {},
}

Check how many fields the enum was declared with and ensure that your pattern uses the same number.

E0025

Each field of a struct can only be bound once in a pattern. Erroneous code example:

struct Foo {
    a: u8,
    b: u8,
}

fn main(){
    let x = Foo { a:1, b:2 };

    let Foo { a: x, a: y } = x;
    // error: field `a` bound multiple times in the pattern
}

Each occurrence of a field name binds the value of that field, so to fix this error you will have to remove or alter the duplicate uses of the field name. Perhaps you misspelled another field name? Example:

struct Foo {
    a: u8,
    b: u8,
}

fn main(){
    let x = Foo { a:1, b:2 };

    let Foo { a: x, b: y } = x; // ok!
}

E0026

This error indicates that a struct pattern attempted to extract a non-existent field from a struct. Struct fields are identified by the name used before the colon : so struct patterns should resemble the declaration of the struct type being matched.

// Correct matching.
struct Thing {
    x: u32,
    y: u32
}

let thing = Thing { x: 1, y: 2 };

match thing {
    Thing { x: xfield, y: yfield } => {}
}

If you are using shorthand field patterns but want to refer to the struct field by a different name, you should rename it explicitly.

Change this:

struct Thing {
    x: u32,
    y: u32
}

let thing = Thing { x: 0, y: 0 };

match thing {
    Thing { x, z } => {}
}

To this:

struct Thing {
    x: u32,
    y: u32
}

let thing = Thing { x: 0, y: 0 };

match thing {
    Thing { x, y: z } => {}
}

E0027

This error indicates that a pattern for a struct fails to specify a sub-pattern for every one of the struct's fields. Ensure that each field from the struct's definition is mentioned in the pattern, or use .. to ignore unwanted fields.

For example:

struct Dog {
    name: String,
    age: u32,
}

let d = Dog { name: "Rusty".to_string(), age: 8 };

// This is incorrect.
match d {
    Dog { age: x } => {}
}

This is correct (explicit):

struct Dog {
    name: String,
    age: u32,
}

let d = Dog { name: "Rusty".to_string(), age: 8 };

match d {
    Dog { name: ref n, age: x } => {}
}

// This is also correct (ignore unused fields).
match d {
    Dog { age: x, .. } => {}
}

E0029

In a match expression, only numbers and characters can be matched against a range. This is because the compiler checks that the range is non-empty at compile-time, and is unable to evaluate arbitrary comparison functions. If you want to capture values of an orderable type between two end-points, you can use a guard.

let string = "salutations !";

// The ordering relation for strings can't be evaluated at compile time,
// so this doesn't work:
match string {
    "hello" ... "world" => {}
    _ => {}
}

// This is a more general version, using a guard:
match string {
    s if s >= "hello" && s <= "world" => {}
    _ => {}
}

E0033

This error indicates that a pointer to a trait type cannot be implicitly dereferenced by a pattern. Every trait defines a type, but because the size of trait implementors isn't fixed, this type has no compile-time size. Therefore, all accesses to trait types must be through pointers. If you encounter this error you should try to avoid dereferencing the pointer.

let trait_obj: &SomeTrait = ...;

// This tries to implicitly dereference to create an unsized local variable.
let &invalid = trait_obj;

// You can call methods without binding to the value being pointed at.
trait_obj.method_one();
trait_obj.method_two();

You can read more about trait objects in the Trait Object section of the Reference:

https://doc.rust-lang.org/reference.html#trait-objects

E0034

The compiler doesn't know what method to call because more than one method has the same prototype. Erroneous code example:

struct Test;

trait Trait1 {
    fn foo();
}

trait Trait2 {
    fn foo();
}

impl Trait1 for Test { fn foo() {} }
impl Trait2 for Test { fn foo() {} }

fn main() {
    Test::foo() // error, which foo() to call?
}

To avoid this error, you have to keep only one of them and remove the others. So let's take our example and fix it:

struct Test;

trait Trait1 {
    fn foo();
}

impl Trait1 for Test { fn foo() {} }

fn main() {
    Test::foo() // and now that's good!
}

However, a better solution would be using fully explicit naming of type and trait:

struct Test;

trait Trait1 {
    fn foo();
}

trait Trait2 {
    fn foo();
}

impl Trait1 for Test { fn foo() {} }
impl Trait2 for Test { fn foo() {} }

fn main() {
    <Test as Trait1>::foo()
}

One last example:

trait F {
    fn m(&self);
}

trait G {
    fn m(&self);
}

struct X;

impl F for X { fn m(&self) { println!("I am F"); } }
impl G for X { fn m(&self) { println!("I am G"); } }

fn main() {
    let f = X;

    F::m(&f); // it displays "I am F"
    G::m(&f); // it displays "I am G"
}

E0035

You tried to give a type parameter where it wasn't needed. Erroneous code example:

struct Test;

impl Test {
    fn method(&self) {}
}

fn main() {
    let x = Test;

    x.method::<i32>(); // Error: Test::method doesn't need type parameter!
}

To fix this error, just remove the type parameter:

struct Test;

impl Test {
    fn method(&self) {}
}

fn main() {
    let x = Test;

    x.method(); // OK, we're good!
}

E0036

This error occurrs when you pass too many or not enough type parameters to a method. Erroneous code example:

struct Test;

impl Test {
    fn method<T>(&self, v: &[T]) -> usize {
        v.len()
    }
}

fn main() {
    let x = Test;
    let v = &[0];

    x.method::<i32, i32>(v); // error: only one type parameter is expected!
}

To fix it, just specify a correct number of type parameters:

struct Test;

impl Test {
    fn method<T>(&self, v: &[T]) -> usize {
        v.len()
    }
}

fn main() {
    let x = Test;
    let v = &[0];

    x.method::<i32>(v); // OK, we're good!
}

Please note on the last example that we could have called method like this:

x.method(v);

E0038

Trait objects like Box<Trait> can only be constructed when certain requirements are satisfied by the trait in question.

Trait objects are a form of dynamic dispatch and use a dynamically sized type for the inner type. So, for a given trait Trait, when Trait is treated as a type, as in Box<Trait>, the inner type is 'unsized'. In such cases the boxed pointer is a 'fat pointer' that contains an extra pointer to a table of methods (among other things) for dynamic dispatch. This design mandates some restrictions on the types of traits that are allowed to be used in trait objects, which are collectively termed as 'object safety' rules.

Attempting to create a trait object for a non object-safe trait will trigger this error.

There are various rules:

The trait cannot require Self: Sized

When Trait is treated as a type, the type does not implement the special Sized trait, because the type does not have a known size at compile time and can only be accessed behind a pointer. Thus, if we have a trait like the following:

trait Foo where Self: Sized {

}

We cannot create an object of type Box<Foo> or &Foo since in this case Self would not be Sized.

Generally, Self : Sized is used to indicate that the trait should not be used as a trait object. If the trait comes from your own crate, consider removing this restriction.

Method references the Self type in its arguments or return type

This happens when a trait has a method like the following:

trait Trait {
    fn foo(&self) -> Self;
}

impl Trait for String {
    fn foo(&self) -> Self {
        "hi".to_owned()
    }
}

impl Trait for u8 {
    fn foo(&self) -> Self {
        1
    }
}

(Note that &self and &mut self are okay, it's additional Self types which cause this problem.)

In such a case, the compiler cannot predict the return type of foo() in a situation like the following:

trait Trait {
    fn foo(&self) -> Self;
}

fn call_foo(x: Box<Trait>) {
    let y = x.foo(); // What type is y?
    // ...
}

If only some methods aren't object-safe, you can add a where Self: Sized bound on them to mark them as explicitly unavailable to trait objects. The functionality will still be available to all other implementers, including Box<Trait> which is itself sized (assuming you impl Trait for Box<Trait>).

trait Trait {
    fn foo(&self) -> Self where Self: Sized;
    // more functions
}

Now, foo() can no longer be called on a trait object, but you will now be allowed to make a trait object, and that will be able to call any object-safe methods. With such a bound, one can still call foo() on types implementing that trait that aren't behind trait objects.

Method has generic type parameters

As mentioned before, trait objects contain pointers to method tables. So, if we have:

trait Trait {
    fn foo(&self);
}

impl Trait for String {
    fn foo(&self) {
        // implementation 1
    }
}

impl Trait for u8 {
    fn foo(&self) {
        // implementation 2
    }
}
// ...

At compile time each implementation of Trait will produce a table containing the various methods (and other items) related to the implementation.

This works fine, but when the method gains generic parameters, we can have a problem.

Usually, generic parameters get monomorphized. For example, if I have

fn foo<T>(x: T) {
    // ...
}

The machine code for foo::<u8>(), foo::<bool>(), foo::<String>(), or any other type substitution is different. Hence the compiler generates the implementation on-demand. If you call foo() with a bool parameter, the compiler will only generate code for foo::<bool>(). When we have additional type parameters, the number of monomorphized implementations the compiler generates does not grow drastically, since the compiler will only generate an implementation if the function is called with unparametrized substitutions (i.e., substitutions where none of the substituted types are themselves parametrized).

However, with trait objects we have to make a table containing every object that implements the trait. Now, if it has type parameters, we need to add implementations for every type that implements the trait, and there could theoretically be an infinite number of types.

For example, with:

trait Trait {
    fn foo<T>(&self, on: T);
    // more methods
}

impl Trait for String {
    fn foo<T>(&self, on: T) {
        // implementation 1
    }
}

impl Trait for u8 {
    fn foo<T>(&self, on: T) {
        // implementation 2
    }
}

// 8 more implementations

Now, if we have the following code:

fn call_foo(thing: Box<Trait>) {
    thing.foo(true); // this could be any one of the 8 types above
    thing.foo(1);
    thing.foo("hello");
}

We don't just need to create a table of all implementations of all methods of Trait, we need to create such a table, for each different type fed to foo(). In this case this turns out to be (10 types implementing Trait)*(3 types being fed to foo()) = 30 implementations!

With real world traits these numbers can grow drastically.

To fix this, it is suggested to use a where Self: Sized bound similar to the fix for the sub-error above if you do not intend to call the method with type parameters:

trait Trait {
    fn foo<T>(&self, on: T) where Self: Sized;
    // more methods
}

If this is not an option, consider replacing the type parameter with another trait object (e.g. if T: OtherTrait, use on: Box<OtherTrait>). If the number of types you intend to feed to this method is limited, consider manually listing out the methods of different types.

Method has no receiver

Methods that do not take a self parameter can't be called since there won't be a way to get a pointer to the method table for them.

trait Foo {
    fn foo() -> u8;
}

This could be called as <Foo as Foo>::foo(), which would not be able to pick an implementation.

Adding a Self: Sized bound to these methods will generally make this compile.

trait Foo {
    fn foo() -> u8 where Self: Sized;
}

The trait cannot use Self as a type parameter in the supertrait listing

This is similar to the second sub-error, but subtler. It happens in situations like the following:

trait Super<A> {}

trait Trait: Super<Self> {
}

struct Foo;

impl Super<Foo> for Foo{}

impl Trait for Foo {}

Here, the supertrait might have methods as follows:

trait Super<A> {
    fn get_a(&self) -> A; // note that this is object safe!
}

If the trait Foo was deriving from something like Super<String> or Super<T> (where Foo itself is Foo<T>), this is okay, because given a type get_a() will definitely return an object of that type.

However, if it derives from Super<Self>, even though Super is object safe, the method get_a() would return an object of unknown type when called on the function. Self type parameters let us make object safe traits no longer safe, so they are forbidden when specifying supertraits.

There's no easy fix for this, generally code will need to be refactored so that you no longer need to derive from Super<Self>.

E0040

It is not allowed to manually call destructors in Rust. It is also not necessary to do this since drop is called automatically whenever a value goes out of scope.

Here's an example of this error:

struct Foo {
    x: i32,
}

impl Drop for Foo {
    fn drop(&mut self) {
        println!("kaboom");
    }
}

fn main() {
    let mut x = Foo { x: -7 };
    x.drop(); // error: explicit use of destructor method
}

E0044

You can't use type parameters on foreign items. Example of erroneous code:

extern { fn some_func<T>(x: T); }

To fix this, replace the type parameter with the specializations that you need:

extern { fn some_func_i32(x: i32); }
extern { fn some_func_i64(x: i64); }

E0045

Rust only supports variadic parameters for interoperability with C code in its FFI. As such, variadic parameters can only be used with functions which are using the C ABI. Examples of erroneous code:

#![feature(unboxed_closures)]

extern "rust-call" { fn foo(x: u8, ...); }

// or

fn foo(x: u8, ...) {}

To fix such code, put them in an extern "C" block:

extern "C" {
    fn foo (x: u8, ...);
}

E0046

Items are missing in a trait implementation. Erroneous code example:

trait Foo {
    fn foo();
}

struct Bar;

impl Foo for Bar {}
// error: not all trait items implemented, missing: `foo`

When trying to make some type implement a trait Foo, you must, at minimum, provide implementations for all of Foo's required methods (meaning the methods that do not have default implementations), as well as any required trait items like associated types or constants. Example:

trait Foo {
    fn foo();
}

struct Bar;

impl Foo for Bar {
    fn foo() {} // ok!
}

E0049

This error indicates that an attempted implementation of a trait method has the wrong number of type parameters.

For example, the trait below has a method foo with a type parameter T, but the implementation of foo for the type Bar is missing this parameter:

trait Foo {
    fn foo<T: Default>(x: T) -> Self;
}

struct Bar;

// error: method `foo` has 0 type parameters but its trait declaration has 1
// type parameter
impl Foo for Bar {
    fn foo(x: bool) -> Self { Bar }
}

E0050

This error indicates that an attempted implementation of a trait method has the wrong number of function parameters.

For example, the trait below has a method foo with two function parameters (&self and u8), but the implementation of foo for the type Bar omits the u8 parameter:

trait Foo {
    fn foo(&self, x: u8) -> bool;
}

struct Bar;

// error: method `foo` has 1 parameter but the declaration in trait `Foo::foo`
// has 2
impl Foo for Bar {
    fn foo(&self) -> bool { true }
}

E0053

The parameters of any trait method must match between a trait implementation and the trait definition.

Here are a couple examples of this error:

trait Foo {
    fn foo(x: u16);
    fn bar(&self);
}

struct Bar;

impl Foo for Bar {
    // error, expected u16, found i16
    fn foo(x: i16) { }

    // error, types differ in mutability
    fn bar(&mut self) { }
}

E0054

It is not allowed to cast to a bool. If you are trying to cast a numeric type to a bool, you can compare it with zero instead:

let x = 5;

// Not allowed, won't compile
let x_is_nonzero = x as bool;
let x = 5;

// Ok
let x_is_nonzero = x != 0;

E0055

During a method call, a value is automatically dereferenced as many times as needed to make the value's type match the method's receiver. The catch is that the compiler will only attempt to dereference a number of times up to the recursion limit (which can be set via the recursion_limit attribute).

For a somewhat artificial example:

#![recursion_limit="2"]

struct Foo;

impl Foo {
    fn foo(&self) {}
}

fn main() {
    let foo = Foo;
    let ref_foo = &&Foo;

    // error, reached the recursion limit while auto-dereferencing &&Foo
    ref_foo.foo();
}

One fix may be to increase the recursion limit. Note that it is possible to create an infinite recursion of dereferencing, in which case the only fix is to somehow break the recursion.

E0057

When invoking closures or other implementations of the function traits Fn, FnMut or FnOnce using call notation, the number of parameters passed to the function must match its definition.

An example using a closure:

let f = |x| x * 3;
let a = f();        // invalid, too few parameters
let b = f(4);       // this works!
let c = f(2, 3);    // invalid, too many parameters

A generic function must be treated similarly:

fn foo<F: Fn()>(f: F) {
    f(); // this is valid, but f(3) would not work
}

E0059

The built-in function traits are generic over a tuple of the function arguments. If one uses angle-bracket notation (Fn<(T,), Output=U>) instead of parentheses (Fn(T) -> U) to denote the function trait, the type parameter should be a tuple. Otherwise function call notation cannot be used and the trait will not be implemented by closures.

The most likely source of this error is using angle-bracket notation without wrapping the function argument type into a tuple, for example:

#![feature(unboxed_closures)]

fn foo<F: Fn<i32>>(f: F) -> F::Output { f(3) }

It can be fixed by adjusting the trait bound like this:

#![feature(unboxed_closures)]

fn foo<F: Fn<(i32,)>>(f: F) -> F::Output { f(3) }

Note that (T,) always denotes the type of a 1-tuple containing an element of type T. The comma is necessary for syntactic disambiguation.

E0060

External C functions are allowed to be variadic. However, a variadic function takes a minimum number of arguments. For example, consider C's variadic printf function:

extern crate libc;
use libc::{ c_char, c_int };

extern "C" {
    fn printf(_: *const c_char, ...) -> c_int;
}

Using this declaration, it must be called with at least one argument, so simply calling printf() is invalid. But the following uses are allowed:

unsafe {
    use std::ffi::CString;

    printf(CString::new("test\n").unwrap().as_ptr());
    printf(CString::new("number = %d\n").unwrap().as_ptr(), 3);
    printf(CString::new("%d, %d\n").unwrap().as_ptr(), 10, 5);
}

E0061

The number of arguments passed to a function must match the number of arguments specified in the function signature.

For example, a function like:

fn f(a: u16, b: &str) {}

Must always be called with exactly two arguments, e.g. f(2, "test").

Note that Rust does not have a notion of optional function arguments or variadic functions (except for its C-FFI).

E0062

This error indicates that during an attempt to build a struct or struct-like enum variant, one of the fields was specified more than once. Erroneous code example:

struct Foo {
    x: i32,
}

fn main() {
    let x = Foo {
                x: 0,
                x: 0, // error: field `x` specified more than once
            };
}

Each field should be specified exactly one time. Example:

struct Foo {
    x: i32,
}

fn main() {
    let x = Foo { x: 0 }; // ok!
}

E0063

This error indicates that during an attempt to build a struct or struct-like enum variant, one of the fields was not provided. Erroneous code example:

struct Foo {
    x: i32,
    y: i32,
}

fn main() {
    let x = Foo { x: 0 }; // error: missing field: `y`
}

Each field should be specified exactly once. Example:

struct Foo {
    x: i32,
    y: i32,
}

fn main() {
    let x = Foo { x: 0, y: 0 }; // ok!
}

E0066

Box placement expressions (like C++'s "placement new") do not yet support any place expression except the exchange heap (i.e. std::boxed::HEAP). Furthermore, the syntax is changing to use in instead of box. See RFC 470 and RFC 809 for more details.

E0067

The left-hand side of a compound assignment expression must be an lvalue expression. An lvalue expression represents a memory location and includes item paths (ie, namespaced variables), dereferences, indexing expressions, and field references.

Let's start with some erroneous code examples:

use std::collections::LinkedList;

// Bad: assignment to non-lvalue expression
LinkedList::new() += 1;

// ...

fn some_func(i: &mut i32) {
    i += 12; // Error : '+=' operation cannot be applied on a reference !
}

And now some working examples:

let mut i : i32 = 0;

i += 12; // Good !

// ...

fn some_func(i: &mut i32) {
    *i += 12; // Good !
}

E0069

The compiler found a function whose body contains a return; statement but whose return type is not (). An example of this is:

// error
fn foo() -> u8 {
    return;
}

Since return; is just like return ();, there is a mismatch between the function's return type and the value being returned.

E0070

The left-hand side of an assignment operator must be an lvalue expression. An lvalue expression represents a memory location and can be a variable (with optional namespacing), a dereference, an indexing expression or a field reference.

More details can be found here: https://doc.rust-lang.org/reference.html#lvalues-rvalues-and-temporaries

Now, we can go further. Here are some erroneous code examples:

struct SomeStruct {
    x: i32,
    y: i32
}

const SOME_CONST : i32 = 12;

fn some_other_func() {}

fn some_function() {
    SOME_CONST = 14; // error : a constant value cannot be changed!
    1 = 3; // error : 1 isn't a valid lvalue!
    some_other_func() = 4; // error : we can't assign value to a function!
    SomeStruct.x = 12; // error : SomeStruct a structure name but it is used
                       // like a variable!
}

And now let's give working examples:

struct SomeStruct {
    x: i32,
    y: i32
}
let mut s = SomeStruct {x: 0, y: 0};

s.x = 3; // that's good !

// ...

fn some_func(x: &mut i32) {
    *x = 12; // that's good !
}

E0071

You tried to use structure-literal syntax to create an item that is not a structure or enum variant.

Example of erroneous code:

type U32 = u32;
let t = U32 { value: 4 }; // error: expected struct, variant or union type,
                          // found builtin type `u32`

To fix this, ensure that the name was correctly spelled, and that the correct form of initializer was used.

For example, the code above can be fixed to:

enum Foo {
    FirstValue(i32)
}

fn main() {
    let u = Foo::FirstValue(0i32);

    let t = 4;
}

E0072

When defining a recursive struct or enum, any use of the type being defined from inside the definition must occur behind a pointer (like Box or &). This is because structs and enums must have a well-defined size, and without the pointer, the size of the type would need to be unbounded.

Consider the following erroneous definition of a type for a list of bytes:

// error, invalid recursive struct type
struct ListNode {
    head: u8,
    tail: Option<ListNode>,
}

This type cannot have a well-defined size, because it needs to be arbitrarily large (since we would be able to nest ListNodes to any depth). Specifically,

size of `ListNode` = 1 byte for `head`
                   + 1 byte for the discriminant of the `Option`
                   + size of `ListNode`

One way to fix this is by wrapping ListNode in a Box, like so:

struct ListNode {
    head: u8,
    tail: Option<Box<ListNode>>,
}

This works because Box is a pointer, so its size is well-known.

E0073

You cannot define a struct (or enum) Foo that requires an instance of Foo in order to make a new Foo value. This is because there would be no way a first instance of Foo could be made to initialize another instance!

Here's an example of a struct that has this problem:

struct Foo { x: Box<Foo> } // error

One fix is to use Option, like so:

struct Foo { x: Option<Box<Foo>> }

Now it's possible to create at least one instance of Foo: Foo { x: None }.

E0074

When using the #[simd] attribute on a tuple struct, the components of the tuple struct must all be of a concrete, nongeneric type so the compiler can reason about how to use SIMD with them. This error will occur if the types are generic.

This will cause an error:

#![feature(repr_simd)]

#[repr(simd)]
struct Bad<T>(T, T, T);

This will not:

#![feature(repr_simd)]

#[repr(simd)]
struct Good(u32, u32, u32);

E0075

The #[simd] attribute can only be applied to non empty tuple structs, because it doesn't make sense to try to use SIMD operations when there are no values to operate on.

This will cause an error:

#![feature(repr_simd)]

#[repr(simd)]
struct Bad;

This will not:

#![feature(repr_simd)]

#[repr(simd)]
struct Good(u32);

E0076

When using the #[simd] attribute to automatically use SIMD operations in tuple struct, the types in the struct must all be of the same type, or the compiler will trigger this error.

This will cause an error:

#![feature(repr_simd)]

#[repr(simd)]
struct Bad(u16, u32, u32);

This will not:

#![feature(repr_simd)]

#[repr(simd)]
struct Good(u32, u32, u32);

E0077

When using the #[simd] attribute on a tuple struct, the elements in the tuple must be machine types so SIMD operations can be applied to them.

This will cause an error:

#![feature(repr_simd)]

#[repr(simd)]
struct Bad(String);

This will not:

#![feature(repr_simd)]

#[repr(simd)]
struct Good(u32, u32, u32);

E0080

This error indicates that the compiler was unable to sensibly evaluate an constant expression that had to be evaluated. Attempting to divide by 0 or causing integer overflow are two ways to induce this error. For example:

enum Enum {
    X = (1 << 500),
    Y = (1 / 0)
}

Ensure that the expressions given can be evaluated as the desired integer type. See the FFI section of the Reference for more information about using a custom integer type:

https://doc.rust-lang.org/reference.html#ffi-attributes

E0081

Enum discriminants are used to differentiate enum variants stored in memory. This error indicates that the same value was used for two or more variants, making them impossible to tell apart.

// Bad.
enum Enum {
    P = 3,
    X = 3,
    Y = 5,
}
// Good.
enum Enum {
    P,
    X = 3,
    Y = 5,
}

Note that variants without a manually specified discriminant are numbered from top to bottom starting from 0, so clashes can occur with seemingly unrelated variants.

enum Bad {
    X,
    Y = 0
}

Here X will have already been specified the discriminant 0 by the time Y is encountered, so a conflict occurs.

E0082

When you specify enum discriminants with =, the compiler expects isize values by default. Or you can add the repr attibute to the enum declaration for an explicit choice of the discriminant type. In either cases, the discriminant values must fall within a valid range for the expected type; otherwise this error is raised. For example:

#[repr(u8)]
enum Thing {
    A = 1024,
    B = 5,
}

Here, 1024 lies outside the valid range for u8, so the discriminant for A is invalid. Here is another, more subtle example which depends on target word size:

enum DependsOnPointerSize {
    A = 1 << 32,
}

Here, 1 << 32 is interpreted as an isize value. So it is invalid for 32 bit target (target_pointer_width = "32") but valid for 64 bit target.

You may want to change representation types to fix this, or else change invalid discriminant values so that they fit within the existing type.

E0084

An unsupported representation was attempted on a zero-variant enum.

Erroneous code example:

#[repr(i32)]
enum NightsWatch {} // error: unsupported representation for zero-variant enum

It is impossible to define an integer type to be used to represent zero-variant enum values because there are no zero-variant enum values. There is no way to construct an instance of the following type using only safe code. So you have two solutions. Either you add variants in your enum:

#[repr(i32)]
enum NightsWatch {
    JonSnow,
    Commander,
}

or you remove the integer represention of your enum:

enum NightsWatch {}

E0087

Too many type parameters were supplied for a function. For example:

fn foo<T>() {}

fn main() {
    foo::<f64, bool>(); // error, expected 1 parameter, found 2 parameters
}

The number of supplied parameters must exactly match the number of defined type parameters.

E0088

You gave too many lifetime parameters. Erroneous code example:

fn f() {}

fn main() {
    f::<'static>() // error: too many lifetime parameters provided
}

Please check you give the right number of lifetime parameters. Example:

fn f() {}

fn main() {
    f() // ok!
}

It's also important to note that the Rust compiler can generally determine the lifetime by itself. Example:

struct Foo {
    value: String
}

impl Foo {
    // it can be written like this
    fn get_value<'a>(&'a self) -> &'a str { &self.value }
    // but the compiler works fine with this too:
    fn without_lifetime(&self) -> &str { &self.value }
}

fn main() {
    let f = Foo { value: "hello".to_owned() };

    println!("{}", f.get_value());
    println!("{}", f.without_lifetime());
}

E0089

Not enough type parameters were supplied for a function. For example:

fn foo<T, U>() {}

fn main() {
    foo::<f64>(); // error, expected 2 parameters, found 1 parameter
}

Note that if a function takes multiple type parameters but you want the compiler to infer some of them, you can use type placeholders:

fn foo<T, U>(x: T) {}

fn main() {
    let x: bool = true;
    foo::<f64>(x);    // error, expected 2 parameters, found 1 parameter
    foo::<_, f64>(x); // same as `foo::<bool, f64>(x)`
}

E0090

You gave too few lifetime parameters. Example:

fn foo<'a: 'b, 'b: 'a>() {}

fn main() {
    foo::<'static>(); // error, expected 2 lifetime parameters
}

Please check you give the right number of lifetime parameters. Example:

fn foo<'a: 'b, 'b: 'a>() {}

fn main() {
    foo::<'static, 'static>();
}

E0091

You gave an unnecessary type parameter in a type alias. Erroneous code example:

type Foo<T> = u32; // error: type parameter `T` is unused
// or:
type Foo<A,B> = Box<A>; // error: type parameter `B` is unused

Please check you didn't write too many type parameters. Example:

type Foo = u32; // ok!
type Foo2<A> = Box<A>; // ok!

E0092

You tried to declare an undefined atomic operation function. Erroneous code example:

#![feature(intrinsics)]

extern "rust-intrinsic" {
    fn atomic_foo(); // error: unrecognized atomic operation
                     //        function
}

Please check you didn't make a mistake in the function's name. All intrinsic functions are defined in librustc_trans/trans/intrinsic.rs and in libcore/intrinsics.rs in the Rust source code. Example:

#![feature(intrinsics)]

extern "rust-intrinsic" {
    fn atomic_fence(); // ok!
}

E0093

You declared an unknown intrinsic function. Erroneous code example:

#![feature(intrinsics)]

extern "rust-intrinsic" {
    fn foo(); // error: unrecognized intrinsic function: `foo`
}

fn main() {
    unsafe {
        foo();
    }
}

Please check you didn't make a mistake in the function's name. All intrinsic functions are defined in librustc_trans/trans/intrinsic.rs and in libcore/intrinsics.rs in the Rust source code. Example:

#![feature(intrinsics)]

extern "rust-intrinsic" {
    fn atomic_fence(); // ok!
}

fn main() {
    unsafe {
        atomic_fence();
    }
}

E0094

You gave an invalid number of type parameters to an intrinsic function. Erroneous code example:

#![feature(intrinsics)]

extern "rust-intrinsic" {
    fn size_of<T, U>() -> usize; // error: intrinsic has wrong number
                                 //        of type parameters
}

Please check that you provided the right number of type parameters and verify with the function declaration in the Rust source code. Example:

#![feature(intrinsics)]

extern "rust-intrinsic" {
    fn size_of<T>() -> usize; // ok!
}

E0106

This error indicates that a lifetime is missing from a type. If it is an error inside a function signature, the problem may be with failing to adhere to the lifetime elision rules (see below).

Here are some simple examples of where you'll run into this error:

struct Foo { x: &bool }        // error
struct Foo<'a> { x: &'a bool } // correct

enum Bar { A(u8), B(&bool), }        // error
enum Bar<'a> { A(u8), B(&'a bool), } // correct

type MyStr = &str;        // error
type MyStr<'a> = &'a str; // correct

Lifetime elision is a special, limited kind of inference for lifetimes in function signatures which allows you to leave out lifetimes in certain cases. For more background on lifetime elision see the book.

The lifetime elision rules require that any function signature with an elided output lifetime must either have

  • exactly one input lifetime
  • or, multiple input lifetimes, but the function must also be a method with a &self or &mut self receiver

In the first case, the output lifetime is inferred to be the same as the unique input lifetime. In the second case, the lifetime is instead inferred to be the same as the lifetime on &self or &mut self.

Here are some examples of elision errors:

// error, no input lifetimes
fn foo() -> &str { }

// error, `x` and `y` have distinct lifetimes inferred
fn bar(x: &str, y: &str) -> &str { }

// error, `y`'s lifetime is inferred to be distinct from `x`'s
fn baz<'a>(x: &'a str, y: &str) -> &str { }

Here's an example that is currently an error, but may work in a future version of Rust:

struct Foo<'a>(&'a str);

trait Quux { }
impl Quux for Foo { }

Lifetime elision in implementation headers was part of the lifetime elision RFC. It is, however, currently unimplemented.

E0107

This error means that an incorrect number of lifetime parameters were provided for a type (like a struct or enum) or trait:

struct Foo<'a, 'b>(&'a str, &'b str);
enum Bar { A, B, C }

struct Baz<'a> {
    foo: Foo<'a>, // error: expected 2, found 1
    bar: Bar<'a>, // error: expected 0, found 1
}

E0109

You tried to give a type parameter to a type which doesn't need it. Erroneous code example:

type X = u32<i32>; // error: type parameters are not allowed on this type

Please check that you used the correct type and recheck its definition. Perhaps it doesn't need the type parameter.

Example:

type X = u32; // this compiles

Note that type parameters for enum-variant constructors go after the variant, not after the enum (Option::None::<u32>, not Option::<u32>::None).

E0110

You tried to give a lifetime parameter to a type which doesn't need it. Erroneous code example:

type X = u32<'static>; // error: lifetime parameters are not allowed on
                       //        this type

Please check that the correct type was used and recheck its definition; perhaps it doesn't need the lifetime parameter. Example:

type X = u32; // ok!

E0116

You can only define an inherent implementation for a type in the same crate where the type was defined. For example, an impl block as below is not allowed since Vec is defined in the standard library:

impl Vec<u8> { } // error

To fix this problem, you can do either of these things:

  • define a trait that has the desired associated functions/types/constants and implement the trait for the type in question
  • define a new type wrapping the type and define an implementation on the new type

Note that using the type keyword does not work here because type only introduces a type alias:

type Bytes = Vec<u8>;

impl Bytes { } // error, same as above

E0117

This error indicates a violation of one of Rust's orphan rules for trait implementations. The rule prohibits any implementation of a foreign trait (a trait defined in another crate) where

  • the type that is implementing the trait is foreign
  • all of the parameters being passed to the trait (if there are any) are also foreign.

Here's one example of this error:

impl Drop for u32 {}

To avoid this kind of error, ensure that at least one local type is referenced by the impl:

pub struct Foo; // you define your type in your crate

impl Drop for Foo { // and you can implement the trait on it!
    // code of trait implementation here
}

impl From<Foo> for i32 { // or you use a type from your crate as
                         // a type parameter
    fn from(i: Foo) -> i32 {
        0
    }
}

Alternatively, define a trait locally and implement that instead:

trait Bar {
    fn get(&self) -> usize;
}

impl Bar for u32 {
    fn get(&self) -> usize { 0 }
}

For information on the design of the orphan rules, see RFC 1023.

E0118

You're trying to write an inherent implementation for something which isn't a struct nor an enum. Erroneous code example:

impl (u8, u8) { // error: no base type found for inherent implementation
    fn get_state(&self) -> String {
        // ...
    }
}

To fix this error, please implement a trait on the type or wrap it in a struct. Example:

// we create a trait here
trait LiveLongAndProsper {
    fn get_state(&self) -> String;
}

// and now you can implement it on (u8, u8)
impl LiveLongAndProsper for (u8, u8) {
    fn get_state(&self) -> String {
        "He's dead, Jim!".to_owned()
    }
}

Alternatively, you can create a newtype. A newtype is a wrapping tuple-struct. For example, NewType is a newtype over Foo in struct NewType(Foo). Example:

struct TypeWrapper((u8, u8));

impl TypeWrapper {
    fn get_state(&self) -> String {
        "Fascinating!".to_owned()
    }
}

E0119

There are conflicting trait implementations for the same type. Example of erroneous code:

trait MyTrait {
    fn get(&self) -> usize;
}

impl<T> MyTrait for T {
    fn get(&self) -> usize { 0 }
}

struct Foo {
    value: usize
}

impl MyTrait for Foo { // error: conflicting implementations of trait
                       //        `MyTrait` for type `Foo`
    fn get(&self) -> usize { self.value }
}

When looking for the implementation for the trait, the compiler finds both the impl<T> MyTrait for T where T is all types and the impl MyTrait for Foo. Since a trait cannot be implemented multiple times, this is an error. So, when you write:

trait MyTrait {
    fn get(&self) -> usize;
}

impl<T> MyTrait for T {
    fn get(&self) -> usize { 0 }
}

This makes the trait implemented on all types in the scope. So if you try to implement it on another one after that, the implementations will conflict. Example:

trait MyTrait {
    fn get(&self) -> usize;
}

impl<T> MyTrait for T {
    fn get(&self) -> usize { 0 }
}

struct Foo;

fn main() {
    let f = Foo;

    f.get(); // the trait is implemented so we can use it
}

E0120

An attempt was made to implement Drop on a trait, which is not allowed: only structs and enums can implement Drop. An example causing this error:

trait MyTrait {}

impl Drop for MyTrait {
    fn drop(&mut self) {}
}

A workaround for this problem is to wrap the trait up in a struct, and implement Drop on that. An example is shown below:

trait MyTrait {}
struct MyWrapper<T: MyTrait> { foo: T }

impl <T: MyTrait> Drop for MyWrapper<T> {
    fn drop(&mut self) {}
}

Alternatively, wrapping trait objects requires something like the following:

trait MyTrait {}

//or Box<MyTrait>, if you wanted an owned trait object
struct MyWrapper<'a> { foo: &'a MyTrait }

impl <'a> Drop for MyWrapper<'a> {
    fn drop(&mut self) {}
}

E0121

In order to be consistent with Rust's lack of global type inference, type placeholders are disallowed by design in item signatures.

Examples of this error include:

fn foo() -> _ { 5 } // error, explicitly write out the return type instead

static BAR: _ = "test"; // error, explicitly write out the type instead

E0122

An attempt was made to add a generic constraint to a type alias. While Rust will allow this with a warning, it will not currently enforce the constraint. Consider the example below:

trait Foo{}

type MyType<R: Foo> = (R, ());

fn main() {
    let t: MyType<u32>;
}

We're able to declare a variable of type MyType<u32>, despite the fact that u32 does not implement Foo. As a result, one should avoid using generic constraints in concert with type aliases.

E0124

You declared two fields of a struct with the same name. Erroneous code example:

struct Foo {
    field1: i32,
    field1: i32, // error: field is already declared
}

Please verify that the field names have been correctly spelled. Example:

struct Foo {
    field1: i32,
    field2: i32, // ok!
}

E0128

Type parameter defaults can only use parameters that occur before them. Erroneous code example:

struct Foo<T=U, U=()> {
    field1: T,
    filed2: U,
}
// error: type parameters with a default cannot use forward declared
// identifiers

Since type parameters are evaluated in-order, you may be able to fix this issue by doing:

struct Foo<U=(), T=U> {
    field1: T,
    filed2: U,
}

Please also verify that this wasn't because of a name-clash and rename the type parameter if so.

E0131

It is not possible to define main with type parameters, or even with function parameters. When main is present, it must take no arguments and return (). Erroneous code example:

fn main<T>() { // error: main function is not allowed to have type parameters
}

E0132

A function with the start attribute was declared with type parameters.

Erroneous code example:

#![feature(start)]

#[start]
fn f<T>() {}

It is not possible to declare type parameters on a function that has the start attribute. Such a function must have the following type signature (for more information: http://doc.rust-lang.org/stable/book/no-stdlib.html):

fn(isize, *const *const u8) -> isize;

Example:

#![feature(start)]

#[start]
fn my_start(argc: isize, argv: *const *const u8) -> isize {
    0
}

E0133

Unsafe code was used outside of an unsafe function or block.

Erroneous code example:

unsafe fn f() { return; } // This is the unsafe code

fn main() {
    f(); // error: call to unsafe function requires unsafe function or block
}

Using unsafe functionality is potentially dangerous and disallowed by safety checks. Examples:

  • Dereferencing raw pointers
  • Calling functions via FFI
  • Calling functions marked unsafe

These safety checks can be relaxed for a section of the code by wrapping the unsafe instructions with an unsafe block. For instance:

unsafe fn f() { return; }

fn main() {
    unsafe { f(); } // ok!
}

See also https://doc.rust-lang.org/book/unsafe.html

E0136

A binary can only have one entry point, and by default that entry point is the function main(). If there are multiple such functions, please rename one.

E0137

More than one function was declared with the #[main] attribute.

Erroneous code example:

#![feature(main)]

#[main]
fn foo() {}

#[main]
fn f() {} // error: multiple functions with a #[main] attribute

This error indicates that the compiler found multiple functions with the #[main] attribute. This is an error because there must be a unique entry point into a Rust program. Example:

#![feature(main)]

#[main]
fn f() {} // ok!

E0138

More than one function was declared with the #[start] attribute.

Erroneous code example:

#![feature(start)]

#[start]
fn foo(argc: isize, argv: *const *const u8) -> isize {}

#[start]
fn f(argc: isize, argv: *const *const u8) -> isize {}
// error: multiple 'start' functions

This error indicates that the compiler found multiple functions with the #[start] attribute. This is an error because there must be a unique entry point into a Rust program. Example:

#![feature(start)]

#[start]
fn foo(argc: isize, argv: *const *const u8) -> isize { 0 } // ok!

E0139

There are various restrictions on transmuting between types in Rust; for example types being transmuted must have the same size. To apply all these restrictions, the compiler must know the exact types that may be transmuted. When type parameters are involved, this cannot always be done.

So, for example, the following is not allowed:

use std::mem::transmute;

struct Foo<T>(Vec<T>);

fn foo<T>(x: Vec<T>) {
    // we are transmuting between Vec<T> and Foo<F> here
    let y: Foo<T> = unsafe { transmute(x) };
    // do something with y
}

In this specific case there's a good chance that the transmute is harmless (but this is not guaranteed by Rust). However, when alignment and enum optimizations come into the picture, it's quite likely that the sizes may or may not match with different type parameter substitutions. It's not possible to check this for all possible types, so transmute() simply only accepts types without any unsubstituted type parameters.

If you need this, there's a good chance you're doing something wrong. Keep in mind that Rust doesn't guarantee much about the layout of different structs (even two structs with identical declarations may have different layouts). If there is a solution that avoids the transmute entirely, try it instead.

If it's possible, hand-monomorphize the code by writing the function for each possible type substitution. It's possible to use traits to do this cleanly, for example:

struct Foo<T>(Vec<T>);

trait MyTransmutableType {
    fn transmute(Vec<Self>) -> Foo<Self>;
}

impl MyTransmutableType for u8 {
    fn transmute(x: Foo<u8>) -> Vec<u8> {
        transmute(x)
    }
}

impl MyTransmutableType for String {
    fn transmute(x: Foo<String>) -> Vec<String> {
        transmute(x)
    }
}

// ... more impls for the types you intend to transmute

fn foo<T: MyTransmutableType>(x: Vec<T>) {
    let y: Foo<T> = <T as MyTransmutableType>::transmute(x);
    // do something with y
}

Each impl will be checked for a size match in the transmute as usual, and since there are no unbound type parameters involved, this should compile unless there is a size mismatch in one of the impls.

It is also possible to manually transmute:

ptr::read(&v as *const _ as *const SomeType) // `v` transmuted to `SomeType`

Note that this does not move v (unlike transmute), and may need a call to mem::forget(v) in case you want to avoid destructors being called.

E0152

A lang item was redefined.

Erroneous code example:

#![feature(lang_items)]

#[lang = "panic_fmt"]
struct Foo; // error: duplicate lang item found: `panic_fmt`

Lang items are already implemented in the standard library. Unless you are writing a free-standing application (e.g. a kernel), you do not need to provide them yourself.

You can build a free-standing crate by adding #![no_std] to the crate attributes:

#![no_std]

See also https://doc.rust-lang.org/book/no-stdlib.html

E0154

Note: this error code is no longer emitted by the compiler.

Imports (use statements) are not allowed after non-item statements, such as variable declarations and expression statements.

Here is an example that demonstrates the error:

fn f() {
    // Variable declaration before import
    let x = 0;
    use std::io::Read;
    // ...
}

The solution is to declare the imports at the top of the block, function, or file.

Here is the previous example again, with the correct order:

fn f() {
    use std::io::Read;
    let x = 0;
    // ...
}

See the Declaration Statements section of the reference for more information about what constitutes an Item declaration and what does not:

https://doc.rust-lang.org/reference.html#statements

E0158

const and static mean different things. A const is a compile-time constant, an alias for a literal value. This property means you can match it directly within a pattern.

The static keyword, on the other hand, guarantees a fixed location in memory. This does not always mean that the value is constant. For example, a global mutex can be declared static as well.

If you want to match against a static, consider using a guard instead:

static FORTY_TWO: i32 = 42;

match Some(42) {
    Some(x) if x == FORTY_TWO => {}
    _ => {}
}

E0162

An if-let pattern attempts to match the pattern, and enters the body if the match was successful. If the match is irrefutable (when it cannot fail to match), use a regular let-binding instead. For instance:

struct Irrefutable(i32);
let irr = Irrefutable(0);

// This fails to compile because the match is irrefutable.
if let Irrefutable(x) = irr {
    // This body will always be executed.
    // ...
}

Try this instead:

struct Irrefutable(i32);
let irr = Irrefutable(0);

let Irrefutable(x) = irr;
println!("{}", x);

E0164

This error means that an attempt was made to match a struct type enum variant as a non-struct type:

enum Foo { B { i: u32 } }

fn bar(foo: Foo) -> u32 {
    match foo {
        Foo::B(i) => i, // error E0164
    }
}

Try using {} instead:

enum Foo { B { i: u32 } }

fn bar(foo: Foo) -> u32 {
    match foo {
        Foo::B{i} => i,
    }
}

E0165

A while-let pattern attempts to match the pattern, and enters the body if the match was successful. If the match is irrefutable (when it cannot fail to match), use a regular let-binding inside a loop instead. For instance:

struct Irrefutable(i32);
let irr = Irrefutable(0);

// This fails to compile because the match is irrefutable.
while let Irrefutable(x) = irr {
    // ...
}

Try this instead:

struct Irrefutable(i32);
let irr = Irrefutable(0);

loop {
    let Irrefutable(x) = irr;
    // ...
}

E0170

Enum variants are qualified by default. For example, given this type:

enum Method {
    GET,
    POST,
}

You would match it using:

enum Method {
    GET,
    POST,
}

let m = Method::GET;

match m {
    Method::GET => {},
    Method::POST => {},
}

If you don't qualify the names, the code will bind new variables named "GET" and "POST" instead. This behavior is likely not what you want, so rustc warns when that happens.

Qualified names are good practice, and most code works well with them. But if you prefer them unqualified, you can import the variants into scope:

use Method::*;
enum Method { GET, POST }

If you want others to be able to import variants from your module directly, use pub use:

pub use Method::*;
enum Method { GET, POST }

E0182

You bound an associated type in an expression path which is not allowed.

Erroneous code example:

trait Foo {
    type A;
    fn bar() -> isize;
}

impl Foo for isize {
    type A = usize;
    fn bar() -> isize { 42 }
}

// error: unexpected binding of associated item in expression path
let x: isize = Foo::<A=usize>::bar();

To give a concrete type when using the Universal Function Call Syntax, use "Type as Trait". Example:

trait Foo {
    type A;
    fn bar() -> isize;
}

impl Foo for isize {
    type A = usize;
    fn bar() -> isize { 42 }
}

let x: isize = <isize as Foo>::bar(); // ok!

E0184

Explicitly implementing both Drop and Copy for a type is currently disallowed. This feature can make some sense in theory, but the current implementation is incorrect and can lead to memory unsafety (see issue #20126), so it has been disabled for now.

E0185

An associated function for a trait was defined to be static, but an implementation of the trait declared the same function to be a method (i.e. to take a self parameter).

Here's an example of this error:

trait Foo {
    fn foo();
}

struct Bar;

impl Foo for Bar {
    // error, method `foo` has a `&self` declaration in the impl, but not in
    // the trait
    fn foo(&self) {}
}

E0186

An associated function for a trait was defined to be a method (i.e. to take a self parameter), but an implementation of the trait declared the same function to be static.

Here's an example of this error:

trait Foo {
    fn foo(&self);
}

struct Bar;

impl Foo for Bar {
    // error, method `foo` has a `&self` declaration in the trait, but not in
    // the impl
    fn foo() {}
}

E0191

Trait objects need to have all associated types specified. Erroneous code example:

trait Trait {
    type Bar;
}

type Foo = Trait; // error: the value of the associated type `Bar` (from
                  //        the trait `Trait`) must be specified

Please verify you specified all associated types of the trait and that you used the right trait. Example:

trait Trait {
    type Bar;
}

type Foo = Trait<Bar=i32>; // ok!

E0192

Negative impls are only allowed for traits with default impls. For more information see the opt-in builtin traits RFC.

E0193

where clauses must use generic type parameters: it does not make sense to use them otherwise. An example causing this error:

trait Foo {
    fn bar(&self);
}

#[derive(Copy,Clone)]
struct Wrapper<T> {
    Wrapped: T
}

impl Foo for Wrapper<u32> where Wrapper<u32>: Clone {
    fn bar(&self) { }
}

This use of a where clause is strange - a more common usage would look something like the following:

trait Foo {
    fn bar(&self);
}

#[derive(Copy,Clone)]
struct Wrapper<T> {
    Wrapped: T
}
impl <T> Foo for Wrapper<T> where Wrapper<T>: Clone {
    fn bar(&self) { }
}

Here, we're saying that the implementation exists on Wrapper only when the wrapped type T implements Clone. The where clause is important because some types will not implement Clone, and thus will not get this method.

In our erroneous example, however, we're referencing a single concrete type. Since we know for certain that Wrapper<u32> implements Clone, there's no reason to also specify it in a where clause.

E0194

A type parameter was declared which shadows an existing one. An example of this error:

trait Foo<T> {
    fn do_something(&self) -> T;
    fn do_something_else<T: Clone>(&self, bar: T);
}

In this example, the trait Foo and the trait method do_something_else both define a type parameter T. This is not allowed: if the method wishes to define a type parameter, it must use a different name for it.

E0195

Your method's lifetime parameters do not match the trait declaration. Erroneous code example:

trait Trait {
    fn bar<'a,'b:'a>(x: &'a str, y: &'b str);
}

struct Foo;

impl Trait for Foo {
    fn bar<'a,'b>(x: &'a str, y: &'b str) {
    // error: lifetime parameters or bounds on method `bar`
    // do not match the trait declaration
    }
}

The lifetime constraint 'b for bar() implementation does not match the trait declaration. Ensure lifetime declarations match exactly in both trait declaration and implementation. Example:

trait Trait {
    fn t<'a,'b:'a>(x: &'a str, y: &'b str);
}

struct Foo;

impl Trait for Foo {
    fn t<'a,'b:'a>(x: &'a str, y: &'b str) { // ok!
    }
}

E0197

Inherent implementations (one that do not implement a trait but provide methods associated with a type) are always safe because they are not implementing an unsafe trait. Removing the unsafe keyword from the inherent implementation will resolve this error.

struct Foo;

// this will cause this error
unsafe impl Foo { }
// converting it to this will fix it
impl Foo { }

E0198

A negative implementation is one that excludes a type from implementing a particular trait. Not being able to use a trait is always a safe operation, so negative implementations are always safe and never need to be marked as unsafe.

#![feature(optin_builtin_traits)]

struct Foo;

// unsafe is unnecessary
unsafe impl !Clone for Foo { }

This will compile:

#![feature(optin_builtin_traits)]

struct Foo;

trait Enterprise {}

impl Enterprise for .. { }

impl !Enterprise for Foo { }

Please note that negative impls are only allowed for traits with default impls.

E0199

Safe traits should not have unsafe implementations, therefore marking an implementation for a safe trait unsafe will cause a compiler error. Removing the unsafe marker on the trait noted in the error will resolve this problem.

struct Foo;

trait Bar { }

// this won't compile because Bar is safe
unsafe impl Bar for Foo { }
// this will compile
impl Bar for Foo { }

E0200

Unsafe traits must have unsafe implementations. This error occurs when an implementation for an unsafe trait isn't marked as unsafe. This may be resolved by marking the unsafe implementation as unsafe.

struct Foo;

unsafe trait Bar { }

// this won't compile because Bar is unsafe and impl isn't unsafe
impl Bar for Foo { }
// this will compile
unsafe impl Bar for Foo { }

E0201

It is an error to define two associated items (like methods, associated types, associated functions, etc.) with the same identifier.

For example:

struct Foo(u8);

impl Foo {
    fn bar(&self) -> bool { self.0 > 5 }
    fn bar() {} // error: duplicate associated function
}

trait Baz {
    type Quux;
    fn baz(&self) -> bool;
}

impl Baz for Foo {
    type Quux = u32;

    fn baz(&self) -> bool { true }

    // error: duplicate method
    fn baz(&self) -> bool { self.0 > 5 }

    // error: duplicate associated type
    type Quux = u32;
}

Note, however, that items with the same name are allowed for inherent impl blocks that don't overlap:

struct Foo<T>(T);

impl Foo<u8> {
    fn bar(&self) -> bool { self.0 > 5 }
}

impl Foo<bool> {
    fn bar(&self) -> bool { self.0 }
}

E0202

Inherent associated types were part of RFC 195 but are not yet implemented. See the tracking issue for the status of this implementation.

E0204

An attempt to implement the Copy trait for a struct failed because one of the fields does not implement Copy. To fix this, you must implement Copy for the mentioned field. Note that this may not be possible, as in the example of

struct Foo {
    foo : Vec<u32>,
}

impl Copy for Foo { }

This fails because Vec<T> does not implement Copy for any T.

Here's another example that will fail:

#[derive(Copy)]
struct Foo<'a> {
    ty: &'a mut bool,
}

This fails because &mut T is not Copy, even when T is Copy (this differs from the behavior for &T, which is always Copy).

E0206

You can only implement Copy for a struct or enum. Both of the following examples will fail, because neither i32 (primitive type) nor &'static Bar (reference to Bar) is a struct or enum:

type Foo = i32;
impl Copy for Foo { } // error

#[derive(Copy, Clone)]
struct Bar;
impl Copy for &'static Bar { } // error

E0207

Any type parameter or lifetime parameter of an impl must meet at least one of the following criteria:

  • it appears in the self type of the impl
  • for a trait impl, it appears in the trait reference
  • it is bound as an associated type

Error example 1

Suppose we have a struct Foo and we would like to define some methods for it. The following definition leads to a compiler error:

struct Foo;

impl<T: Default> Foo {
// error: the type parameter `T` is not constrained by the impl trait, self
// type, or predicates [E0207]
    fn get(&self) -> T {
        <T as Default>::default()
    }
}

The problem is that the parameter T does not appear in the self type (Foo) of the impl. In this case, we can fix the error by moving the type parameter from the impl to the method get:

struct Foo;

// Move the type parameter from the impl to the method
impl Foo {
    fn get<T: Default>(&self) -> T {
        <T as Default>::default()
    }
}

Error example 2

As another example, suppose we have a Maker trait and want to establish a type FooMaker that makes Foos:

trait Maker {
    type Item;
    fn make(&mut self) -> Self::Item;
}

struct Foo<T> {
    foo: T
}

struct FooMaker;

impl<T: Default> Maker for FooMaker {
// error: the type parameter `T` is not constrained by the impl trait, self
// type, or predicates [E0207]
    type Item = Foo<T>;

    fn make(&mut self) -> Foo<T> {
        Foo { foo: <T as Default>::default() }
    }
}

This fails to compile because T does not appear in the trait or in the implementing type.

One way to work around this is to introduce a phantom type parameter into FooMaker, like so:

use std::marker::PhantomData;

trait Maker {
    type Item;
    fn make(&mut self) -> Self::Item;
}

struct Foo<T> {
    foo: T
}

// Add a type parameter to `FooMaker`
struct FooMaker<T> {
    phantom: PhantomData<T>,
}

impl<T: Default> Maker for FooMaker<T> {
    type Item = Foo<T>;

    fn make(&mut self) -> Foo<T> {
        Foo {
            foo: <T as Default>::default(),
        }
    }
}

Another way is to do away with the associated type in Maker and use an input type parameter instead:

// Use a type parameter instead of an associated type here
trait Maker<Item> {
    fn make(&mut self) -> Item;
}

struct Foo<T> {
    foo: T
}

struct FooMaker;

impl<T: Default> Maker<Foo<T>> for FooMaker {
    fn make(&mut self) -> Foo<T> {
        Foo { foo: <T as Default>::default() }
    }
}

Additional information

For more information, please see RFC 447.

E0210

This error indicates a violation of one of Rust's orphan rules for trait implementations. The rule concerns the use of type parameters in an implementation of a foreign trait (a trait defined in another crate), and states that type parameters must be "covered" by a local type. To understand what this means, it is perhaps easiest to consider a few examples.

If ForeignTrait is a trait defined in some external crate foo, then the following trait impl is an error:

extern crate collections;
use collections::range::RangeArgument;

impl<T> RangeArgument<T> for T { } // error

fn main() {}

To work around this, it can be covered with a local type, MyType:

struct MyType<T>(T);
impl<T> ForeignTrait for MyType<T> { } // Ok

Please note that a type alias is not sufficient.

For another example of an error, suppose there's another trait defined in foo named ForeignTrait2 that takes two type parameters. Then this impl results in the same rule violation:

struct MyType2;
impl<T> ForeignTrait2<T, MyType<T>> for MyType2 { } // error

The reason for this is that there are two appearances of type parameter T in the impl header, both as parameters for ForeignTrait2. The first appearance is uncovered, and so runs afoul of the orphan rule.

Consider one more example:

impl<T> ForeignTrait2<MyType<T>, T> for MyType2 { } // Ok

This only differs from the previous impl in that the parameters T and MyType<T> for ForeignTrait2 have been swapped. This example does not violate the orphan rule; it is permitted.

To see why that last example was allowed, you need to understand the general rule. Unfortunately this rule is a bit tricky to state. Consider an impl:

impl<P1, ..., Pm> ForeignTrait<T1, ..., Tn> for T0 { ... }

where P1, ..., Pm are the type parameters of the impl and T0, ..., Tn are types. One of the types T0, ..., Tn must be a local type (this is another orphan rule, see the explanation for E0117). Let i be the smallest integer such that Ti is a local type. Then no type parameter can appear in any of the Tj for j < i.

For information on the design of the orphan rules, see RFC 1023.

E0214

A generic type was described using parentheses rather than angle brackets. For example:

fn main() {
    let v: Vec(&str) = vec!["foo"];
}

This is not currently supported: v should be defined as Vec<&str>. Parentheses are currently only used with generic types when defining parameters for Fn-family traits.

E0220

You used an associated type which isn't defined in the trait. Erroneous code example:

trait T1 {
    type Bar;
}

type Foo = T1<F=i32>; // error: associated type `F` not found for `T1`

// or:

trait T2 {
    type Bar;

    // error: Baz is used but not declared
    fn return_bool(&self, &Self::Bar, &Self::Baz) -> bool;
}

Make sure that you have defined the associated type in the trait body. Also, verify that you used the right trait or you didn't misspell the associated type name. Example:

trait T1 {
    type Bar;
}

type Foo = T1<Bar=i32>; // ok!

// or:

trait T2 {
    type Bar;
    type Baz; // we declare `Baz` in our trait.

    // and now we can use it here:
    fn return_bool(&self, &Self::Bar, &Self::Baz) -> bool;
}

E0221

An attempt was made to retrieve an associated type, but the type was ambiguous. For example:

trait T1 {}
trait T2 {}

trait Foo {
    type A: T1;
}

trait Bar : Foo {
    type A: T2;
    fn do_something() {
        let _: Self::A;
    }
}

In this example, Foo defines an associated type A. Bar inherits that type from Foo, and defines another associated type of the same name. As a result, when we attempt to use Self::A, it's ambiguous whether we mean the A defined by Foo or the one defined by Bar.

There are two options to work around this issue. The first is simply to rename one of the types. Alternatively, one can specify the intended type using the following syntax:

trait T1 {}
trait T2 {}

trait Foo {
    type A: T1;
}

trait Bar : Foo {
    type A: T2;
    fn do_something() {
        let _: <Self as Bar>::A;
    }
}

E0223

An attempt was made to retrieve an associated type, but the type was ambiguous. For example:

trait MyTrait {type X; }

fn main() {
    let foo: MyTrait::X;
}

The problem here is that we're attempting to take the type of X from MyTrait. Unfortunately, the type of X is not defined, because it's only made concrete in implementations of the trait. A working version of this code might look like:

trait MyTrait {type X; }
struct MyStruct;

impl MyTrait for MyStruct {
    type X = u32;
}

fn main() {
    let foo: <MyStruct as MyTrait>::X;
}

This syntax specifies that we want the X type from MyTrait, as made concrete in MyStruct. The reason that we cannot simply use MyStruct::X is that MyStruct might implement two different traits with identically-named associated types. This syntax allows disambiguation between the two.

E0225

You attempted to use multiple types as bounds for a closure or trait object. Rust does not currently support this. A simple example that causes this error:

fn main() {
    let _: Box<std::io::Read + std::io::Write>;
}

Send and Sync are an exception to this rule: it's possible to have bounds of one non-builtin trait, plus either or both of Send and Sync. For example, the following compiles correctly:

fn main() {
    let _: Box<std::io::Read + Send + Sync>;
}

E0229

An associated type binding was done outside of the type parameter declaration and where clause. Erroneous code example:

pub trait Foo {
    type A;
    fn boo(&self) -> <Self as Foo>::A;
}

struct Bar;

impl Foo for isize {
    type A = usize;
    fn boo(&self) -> usize { 42 }
}

fn baz<I>(x: &<I as Foo<A=Bar>>::A) {}
// error: associated type bindings are not allowed here

To solve this error, please move the type bindings in the type parameter declaration:

fn baz<I: Foo<A=Bar>>(x: &<I as Foo>::A) {} // ok!

Or in the where clause:

fn baz<I>(x: &<I as Foo>::A) where I: Foo<A=Bar> {}

E0230

The trait has more type parameters specified than appear in its definition.

Erroneous example code:

#![feature(on_unimplemented)]
#[rustc_on_unimplemented = "Trait error on `{Self}` with `<{A},{B},{C}>`"]
// error: there is no type parameter C on trait TraitWithThreeParams
trait TraitWithThreeParams<A,B>
{}

Include the correct number of type parameters and the compilation should proceed:

#![feature(on_unimplemented)]
#[rustc_on_unimplemented = "Trait error on `{Self}` with `<{A},{B},{C}>`"]
trait TraitWithThreeParams<A,B,C> // ok!
{}

E0232

The attribute must have a value. Erroneous code example:

#![feature(on_unimplemented)]

#[rustc_on_unimplemented] // error: this attribute must have a value
trait Bar {}

Please supply the missing value of the attribute. Example:

#![feature(on_unimplemented)]

#[rustc_on_unimplemented = "foo"] // ok!
trait Bar {}

E0243

This error indicates that not enough type parameters were found in a type or trait.

For example, the Foo struct below is defined to be generic in T, but the type parameter is missing in the definition of Bar:

struct Foo<T> { x: T }

struct Bar { x: Foo }

E0244

This error indicates that too many type parameters were found in a type or trait.

For example, the Foo struct below has no type parameters, but is supplied with two in the definition of Bar:

struct Foo { x: bool }

struct Bar<S, T> { x: Foo<S, T> }

E0251

Note: this error code is no longer emitted by the compiler.

Two items of the same name cannot be imported without rebinding one of the items under a new local name.

An example of this error:

use foo::baz;
use bar::*; // error, do `use foo::baz as quux` instead on the previous line

fn main() {}

mod foo {
    pub struct baz;
}

mod bar {
    pub mod baz {}
}

E0252

Two items of the same name cannot be imported without rebinding one of the items under a new local name.

Erroneous code example:

use foo::baz;
use bar::baz; // error, do `use bar::baz as quux` instead

fn main() {}

mod foo {
    pub struct baz;
}

mod bar {
    pub mod baz {}
}

You can use aliases in order to fix this error. Example:

use foo::baz as foo_baz;
use bar::baz; // ok!

fn main() {}

mod foo {
    pub struct baz;
}

mod bar {
    pub mod baz {}
}

Or you can reference the item with its parent:

use bar::baz;

fn main() {
    let x = foo::baz; // ok!
}

mod foo {
    pub struct baz;
}

mod bar {
    pub mod baz {}
}

E0253

Attempt was made to import an unimportable value. This can happen when trying to import a method from a trait.

Erroneous code example:

mod foo {
    pub trait MyTrait {
        fn do_something();
    }
}

use foo::MyTrait::do_something;
// error: `do_something` is not directly importable

fn main() {}

It's invalid to directly import methods belonging to a trait or concrete type.

E0254

Attempt was made to import an item whereas an extern crate with this name has already been imported.

Erroneous code example:

extern crate collections;

mod foo {
    pub trait collections {
        fn do_something();
    }
}

use foo::collections; // error: an extern crate named `collections` has already
                      //        been imported in this module

fn main() {}

To fix issue issue, you have to rename at least one of the two imports. Example:

extern crate collections as libcollections; // ok!

mod foo {
    pub trait collections {
        fn do_something();
    }
}

use foo::collections;

fn main() {}

E0255

You can't import a value whose name is the same as another value defined in the module.

Erroneous code example:

use bar::foo; // error: an item named `foo` is already in scope

fn foo() {}

mod bar {
     pub fn foo() {}
}

fn main() {}

You can use aliases in order to fix this error. Example:

use bar::foo as bar_foo; // ok!

fn foo() {}

mod bar {
     pub fn foo() {}
}

fn main() {}

Or you can reference the item with its parent:

fn foo() {}

mod bar {
     pub fn foo() {}
}

fn main() {
    bar::foo(); // we get the item by referring to its parent
}

E0256

Note: this error code is no longer emitted by the compiler.

You can't import a type or module when the name of the item being imported is the same as another type or submodule defined in the module.

An example of this error:

use foo::Bar; // error

type Bar = u32;

mod foo {
    pub mod Bar { }
}

fn main() {}

E0259

The name chosen for an external crate conflicts with another external crate that has been imported into the current module.

Erroneous code example:

extern crate std;
extern crate libc as std;

fn main() {}

The solution is to choose a different name that doesn't conflict with any external crate imported into the current module.

Correct example:

extern crate std;
extern crate libc as other_name;

E0260

The name for an item declaration conflicts with an external crate's name.

Erroneous code example:

extern crate abc;

struct abc;

There are two possible solutions:

Solution #1: Rename the item.

extern crate abc;

struct xyz;

Solution #2: Import the crate with a different name.

extern crate abc as xyz;

struct abc;

See the Declaration Statements section of the reference for more information about what constitutes an Item declaration and what does not:

https://doc.rust-lang.org/reference.html#statements

E0261

When using a lifetime like 'a in a type, it must be declared before being used.

These two examples illustrate the problem:

// error, use of undeclared lifetime name `'a`
fn foo(x: &'a str) { }

struct Foo {
    // error, use of undeclared lifetime name `'a`
    x: &'a str,
}

These can be fixed by declaring lifetime parameters:

fn foo<'a>(x: &'a str) {}

struct Foo<'a> {
    x: &'a str,
}

E0262

Declaring certain lifetime names in parameters is disallowed. For example, because the 'static lifetime is a special built-in lifetime name denoting the lifetime of the entire program, this is an error:

// error, invalid lifetime parameter name `'static`
fn foo<'static>(x: &'static str) { }

E0263

A lifetime name cannot be declared more than once in the same scope. For example:

// error, lifetime name `'a` declared twice in the same scope
fn foo<'a, 'b, 'a>(x: &'a str, y: &'b str) { }

E0264

An unknown external lang item was used. Erroneous code example:

#![feature(lang_items)]

extern "C" {
    #[lang = "cake"] // error: unknown external lang item: `cake`
    fn cake();
}

A list of available external lang items is available in src/librustc/middle/weak_lang_items.rs. Example:

#![feature(lang_items)]

extern "C" {
    #[lang = "panic_fmt"] // ok!
    fn cake();
}

E0271

This is because of a type mismatch between the associated type of some trait (e.g. T::Bar, where T implements trait Quux { type Bar; }) and another type U that is required to be equal to T::Bar, but is not. Examples follow.

Here is a basic example:

trait Trait { type AssociatedType; }

fn foo<T>(t: T) where T: Trait<AssociatedType=u32> {
    println!("in foo");
}

impl Trait for i8 { type AssociatedType = &'static str; }

foo(3_i8);

Here is that same example again, with some explanatory comments:

trait Trait { type AssociatedType; }

fn foo<T>(t: T) where T: Trait<AssociatedType=u32> {
//                    ~~~~~~~~ ~~~~~~~~~~~~~~~~~~
//                        |            |
//         This says `foo` can         |
//           only be used with         |
//              some type that         |
//         implements `Trait`.         |
//                                     |
//                             This says not only must
//                             `T` be an impl of `Trait`
//                             but also that the impl
//                             must assign the type `u32`
//                             to the associated type.
    println!("in foo");
}

impl Trait for i8 { type AssociatedType = &'static str; }
~~~~~~~~~~~~~~~~~   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//      |                             |
// `i8` does have                     |
// implementation                     |
// of `Trait`...                      |
//                     ... but it is an implementation
//                     that assigns `&'static str` to
//                     the associated type.

foo(3_i8);
// Here, we invoke `foo` with an `i8`, which does not satisfy
// the constraint `<i8 as Trait>::AssociatedType=u32`, and
// therefore the type-checker complains with this error code.

Here is a more subtle instance of the same problem, that can arise with for-loops in Rust:

let vs: Vec<i32> = vec![1, 2, 3, 4];
for v in &vs {
    match v {
        1 => {},
        _ => {},
    }
}

The above fails because of an analogous type mismatch, though may be harder to see. Again, here are some explanatory comments for the same example:

{
    let vs = vec![1, 2, 3, 4];

    // `for`-loops use a protocol based on the `Iterator`
    // trait. Each item yielded in a `for` loop has the
    // type `Iterator::Item` -- that is, `Item` is the
    // associated type of the concrete iterator impl.
    for v in &vs {
//      ~    ~~~
//      |     |
//      |    We borrow `vs`, iterating over a sequence of
//      |    *references* of type `&Elem` (where `Elem` is
//      |    vector's element type). Thus, the associated
//      |    type `Item` must be a reference `&`-type ...
//      |
//  ... and `v` has the type `Iterator::Item`, as dictated by
//  the `for`-loop protocol ...

        match v {
            1 => {}
//          ~
//          |
// ... but *here*, `v` is forced to have some integral type;
// only types like `u8`,`i8`,`u16`,`i16`, et cetera can
// match the pattern `1` ...

            _ => {}
        }

// ... therefore, the compiler complains, because it sees
// an attempt to solve the equations
// `some integral-type` = type-of-`v`
//                      = `Iterator::Item`
//                      = `&Elem` (i.e. `some reference type`)
//
// which cannot possibly all be true.

    }
}

To avoid those issues, you have to make the types match correctly. So we can fix the previous examples like this:

// Basic Example:
trait Trait { type AssociatedType; }

fn foo<T>(t: T) where T: Trait<AssociatedType = &'static str> {
    println!("in foo");
}

impl Trait for i8 { type AssociatedType = &'static str; }

foo(3_i8);

// For-Loop Example:
let vs = vec![1, 2, 3, 4];
for v in &vs {
    match v {
        &1 => {}
        _ => {}
    }
}

E0272

The #[rustc_on_unimplemented] attribute lets you specify a custom error message for when a particular trait isn't implemented on a type placed in a position that needs that trait. For example, when the following code is compiled:

#![feature(on_unimplemented)]

fn foo<T: Index<u8>>(x: T){}

#[rustc_on_unimplemented = "the type `{Self}` cannot be indexed by `{Idx}`"]
trait Index<Idx> { /* ... */ }

foo(true); // `bool` does not implement `Index<u8>`

There will be an error about bool not implementing Index<u8>, followed by a note saying "the type bool cannot be indexed by u8".

As you can see, you can specify type parameters in curly braces for substitution with the actual types (using the regular format string syntax) in a given situation. Furthermore, {Self} will substitute to the type (in this case, bool) that we tried to use.

This error appears when the curly braces contain an identifier which doesn't match with any of the type parameters or the string Self. This might happen if you misspelled a type parameter, or if you intended to use literal curly braces. If it is the latter, escape the curly braces with a second curly brace of the same type; e.g. a literal { is {{.

E0273

The #[rustc_on_unimplemented] attribute lets you specify a custom error message for when a particular trait isn't implemented on a type placed in a position that needs that trait. For example, when the following code is compiled:

#![feature(on_unimplemented)]

fn foo<T: Index<u8>>(x: T){}

#[rustc_on_unimplemented = "the type `{Self}` cannot be indexed by `{Idx}`"]
trait Index<Idx> { /* ... */ }

foo(true); // `bool` does not implement `Index<u8>`

there will be an error about bool not implementing Index<u8>, followed by a note saying "the type bool cannot be indexed by u8".

As you can see, you can specify type parameters in curly braces for substitution with the actual types (using the regular format string syntax) in a given situation. Furthermore, {Self} will substitute to the type (in this case, bool) that we tried to use.

This error appears when the curly braces do not contain an identifier. Please add one of the same name as a type parameter. If you intended to use literal braces, use {{ and }} to escape them.

E0274

The #[rustc_on_unimplemented] attribute lets you specify a custom error message for when a particular trait isn't implemented on a type placed in a position that needs that trait. For example, when the following code is compiled:

#![feature(on_unimplemented)]

fn foo<T: Index<u8>>(x: T){}

#[rustc_on_unimplemented = "the type `{Self}` cannot be indexed by `{Idx}`"]
trait Index<Idx> { /* ... */ }

foo(true); // `bool` does not implement `Index<u8>`

there will be an error about bool not implementing Index<u8>, followed by a note saying "the type bool cannot be indexed by u8".

For this to work, some note must be specified. An empty attribute will not do anything, please remove the attribute or add some helpful note for users of the trait.

E0275

This error occurs when there was a recursive trait requirement that overflowed before it could be evaluated. Often this means that there is unbounded recursion in resolving some type bounds.

For example, in the following code:

trait Foo {}

struct Bar<T>(T);

impl<T> Foo for T where Bar<T>: Foo {}

To determine if a T is Foo, we need to check if Bar<T> is Foo. However, to do this check, we need to determine that Bar<Bar<T>> is Foo. To determine this, we check if Bar<Bar<Bar<T>>> is Foo, and so on. This is clearly a recursive requirement that can't be resolved directly.

Consider changing your trait bounds so that they're less self-referential.

E0276

This error occurs when a bound in an implementation of a trait does not match the bounds specified in the original trait. For example:

trait Foo {
    fn foo<T>(x: T);
}

impl Foo for bool {
    fn foo<T>(x: T) where T: Copy {}
}

Here, all types implementing Foo must have a method foo<T>(x: T) which can take any type T. However, in the impl for bool, we have added an extra bound that T is Copy, which isn't compatible with the original trait.

Consider removing the bound from the method or adding the bound to the original method definition in the trait.

E0277

You tried to use a type which doesn't implement some trait in a place which expected that trait. Erroneous code example:

// here we declare the Foo trait with a bar method
trait Foo {
    fn bar(&self);
}

// we now declare a function which takes an object implementing the Foo trait
fn some_func<T: Foo>(foo: T) {
    foo.bar();
}

fn main() {
    // we now call the method with the i32 type, which doesn't implement
    // the Foo trait
    some_func(5i32); // error: the trait bound `i32 : Foo` is not satisfied
}

In order to fix this error, verify that the type you're using does implement the trait. Example:

trait Foo {
    fn bar(&self);
}

fn some_func<T: Foo>(foo: T) {
    foo.bar(); // we can now use this method since i32 implements the
               // Foo trait
}

// we implement the trait on the i32 type
impl Foo for i32 {
    fn bar(&self) {}
}

fn main() {
    some_func(5i32); // ok!
}

Or in a generic context, an erroneous code example would look like:

fn some_func<T>(foo: T) {
    println!("{:?}", foo); // error: the trait `core::fmt::Debug` is not
                           //        implemented for the type `T`
}

fn main() {
    // We now call the method with the i32 type,
    // which *does* implement the Debug trait.
    some_func(5i32);
}

Note that the error here is in the definition of the generic function: Although we only call it with a parameter that does implement Debug, the compiler still rejects the function: It must work with all possible input types. In order to make this example compile, we need to restrict the generic type we're accepting:

use std::fmt;

// Restrict the input type to types that implement Debug.
fn some_func<T: fmt::Debug>(foo: T) {
    println!("{:?}", foo);
}

fn main() {
    // Calling the method is still fine, as i32 implements Debug.
    some_func(5i32);

    // This would fail to compile now:
    // struct WithoutDebug;
    // some_func(WithoutDebug);
}

Rust only looks at the signature of the called function, as such it must already specify all requirements that will be used for every type parameter.

E0281

You tried to supply a type which doesn't implement some trait in a location which expected that trait. This error typically occurs when working with Fn-based types. Erroneous code example:

fn foo<F: Fn()>(x: F) { }

fn main() {
    // type mismatch: the type ... implements the trait `core::ops::Fn<(_,)>`,
    // but the trait `core::ops::Fn<()>` is required (expected (), found tuple
    // [E0281]
    foo(|y| { });
}

The issue in this case is that foo is defined as accepting a Fn with no arguments, but the closure we attempted to pass to it requires one argument.

E0282

This error indicates that type inference did not result in one unique possible type, and extra information is required. In most cases this can be provided by adding a type annotation. Sometimes you need to specify a generic type parameter manually.

A common example is the collect method on Iterator. It has a generic type parameter with a FromIterator bound, which for a char iterator is implemented by Vec and String among others. Consider the following snippet that reverses the characters of a string:

let x = "hello".chars().rev().collect();

In this case, the compiler cannot infer what the type of x should be: Vec<char> and String are both suitable candidates. To specify which type to use, you can use a type annotation on x:

let x: Vec<char> = "hello".chars().rev().collect();

It is not necessary to annotate the full type. Once the ambiguity is resolved, the compiler can infer the rest:

let x: Vec<_> = "hello".chars().rev().collect();

Another way to provide the compiler with enough information, is to specify the generic type parameter:

let x = "hello".chars().rev().collect::<Vec<char>>();

Again, you need not specify the full type if the compiler can infer it:

let x = "hello".chars().rev().collect::<Vec<_>>();

Apart from a method or function with a generic type parameter, this error can occur when a type parameter of a struct or trait cannot be inferred. In that case it is not always possible to use a type annotation, because all candidates have the same return type. For instance:

struct Foo<T> {
    num: T,
}

impl<T> Foo<T> {
    fn bar() -> i32 {
        0
    }

    fn baz() {
        let number = Foo::bar();
    }
}

This will fail because the compiler does not know which instance of Foo to call bar on. Change Foo::bar() to Foo::<T>::bar() to resolve the error.

E0283

This error occurs when the compiler doesn't have enough information to unambiguously choose an implementation.

For example:

trait Generator {
    fn create() -> u32;
}

struct Impl;

impl Generator for Impl {
    fn create() -> u32 { 1 }
}

struct AnotherImpl;

impl Generator for AnotherImpl {
    fn create() -> u32 { 2 }
}

fn main() {
    let cont: u32 = Generator::create();
    // error, impossible to choose one of Generator trait implementation
    // Impl or AnotherImpl? Maybe anything else?
}

To resolve this error use the concrete type:

trait Generator {
    fn create() -> u32;
}

struct AnotherImpl;

impl Generator for AnotherImpl {
    fn create() -> u32 { 2 }
}

fn main() {
    let gen1 = AnotherImpl::create();

    // if there are multiple methods with same name (different traits)
    let gen2 = <AnotherImpl as Generator>::create();
}

E0296

This error indicates that the given recursion limit could not be parsed. Ensure that the value provided is a positive integer between quotes.

Erroneous code example:

#![recursion_limit]

fn main() {}

And a working example:

#![recursion_limit="1000"]

fn main() {}

E0297

Patterns used to bind names must be irrefutable. That is, they must guarantee that a name will be extracted in all cases. Instead of pattern matching the loop variable, consider using a match or if let inside the loop body. For instance:

let xs : Vec<Option<i32>> = vec![Some(1), None];

// This fails because `None` is not covered.
for Some(x) in xs {
    // ...
}

Match inside the loop instead:

let xs : Vec<Option<i32>> = vec![Some(1), None];

for item in xs {
    match item {
        Some(x) => {},
        None => {},
    }
}

Or use if let:

let xs : Vec<Option<i32>> = vec![Some(1), None];

for item in xs {
    if let Some(x) = item {
        // ...
    }
}

E0301

Mutable borrows are not allowed in pattern guards, because matching cannot have side effects. Side effects could alter the matched object or the environment on which the match depends in such a way, that the match would not be exhaustive. For instance, the following would not match any arm if mutable borrows were allowed:

match Some(()) {
    None => { },
    option if option.take().is_none() => {
        /* impossible, option is `Some` */
    },
    Some(_) => { } // When the previous match failed, the option became `None`.
}

E0302

Assignments are not allowed in pattern guards, because matching cannot have side effects. Side effects could alter the matched object or the environment on which the match depends in such a way, that the match would not be exhaustive. For instance, the following would not match any arm if assignments were allowed:

match Some(()) {
    None => { },
    option if { option = None; false } => { },
    Some(_) => { } // When the previous match failed, the option became `None`.
}

E0303

In certain cases it is possible for sub-bindings to violate memory safety. Updates to the borrow checker in a future version of Rust may remove this restriction, but for now patterns must be rewritten without sub-bindings.

Before:

match Some("hi".to_string()) {
    ref op_string_ref @ Some(s) => {},
    None => {},
}

After:

match Some("hi".to_string()) {
    Some(ref s) => {
        let op_string_ref = &Some(s);
        // ...
    },
    None => {},
}

The op_string_ref binding has type &Option<&String> in both cases.

See also https://github.com/rust-lang/rust/issues/14587

E0308

This error occurs when the compiler was unable to infer the concrete type of a variable. It can occur for several cases, the most common of which is a mismatch in the expected type that the compiler inferred for a variable's initializing expression, and the actual type explicitly assigned to the variable.

For example:

let x: i32 = "I am not a number!";
//     ~~~   ~~~~~~~~~~~~~~~~~~~~
//      |             |
//      |    initializing expression;
//      |    compiler infers type `&str`
//      |
//    type `i32` assigned to variable `x`

E0309

Types in type definitions have lifetimes associated with them that represent how long the data stored within them is guaranteed to be live. This lifetime must be as long as the data needs to be alive, and missing the constraint that denotes this will cause this error.

// This won't compile because T is not constrained, meaning the data
// stored in it is not guaranteed to last as long as the reference
struct Foo<'a, T> {
    foo: &'a T
}

This will compile, because it has the constraint on the type parameter:

struct Foo<'a, T: 'a> {
    foo: &'a T
}

To see why this is important, consider the case where T is itself a reference (e.g., T = &str). If we don't include the restriction that T: 'a, the following code would be perfectly legal:

struct Foo<'a, T> {
    foo: &'a T
}

fn main() {
    let v = "42".to_string();
    let f = Foo{foo: &v};
    drop(v);
    println!("{}", f.foo); // but we've already dropped v!
}

E0310

Types in type definitions have lifetimes associated with them that represent how long the data stored within them is guaranteed to be live. This lifetime must be as long as the data needs to be alive, and missing the constraint that denotes this will cause this error.

// This won't compile because T is not constrained to the static lifetime
// the reference needs
struct Foo<T> {
    foo: &'static T
}

This will compile, because it has the constraint on the type parameter:

struct Foo<T: 'static> {
    foo: &'static T
}

E0312

A lifetime of reference outlives lifetime of borrowed content.

Erroneous code example:

fn make_child<'human, 'elve>(x: &mut &'human isize, y: &mut &'elve isize) {
    *x = *y;
    // error: lifetime of reference outlives lifetime of borrowed content
}

The compiler cannot determine if the human lifetime will live long enough to keep up on the elve one. To solve this error, you have to give an explicit lifetime hierarchy:

fn make_child<'human, 'elve: 'human>(x: &mut &'human isize,
                                     y: &mut &'elve isize) {
    *x = *y; // ok!
}

Or use the same lifetime for every variable:

fn make_child<'elve>(x: &mut &'elve isize, y: &mut &'elve isize) {
    *x = *y; // ok!
}

E0317

This error occurs when an if expression without an else block is used in a context where a type other than () is expected, for example a let expression:

fn main() {
    let x = 5;
    let a = if x == 5 { 1 };
}

An if expression without an else block has the type (), so this is a type error. To resolve it, add an else block having the same type as the if block.

E0318

Default impls for a trait must be located in the same crate where the trait was defined. For more information see the opt-in builtin traits RFC.

E0321

A cross-crate opt-out trait was implemented on something which wasn't a struct or enum type. Erroneous code example:

#![feature(optin_builtin_traits)]

struct Foo;

impl !Sync for Foo {}

unsafe impl Send for &'static Foo {}
// error: cross-crate traits with a default impl, like `core::marker::Send`,
//        can only be implemented for a struct/enum type, not
//        `&'static Foo`

Only structs and enums are permitted to impl Send, Sync, and other opt-out trait, and the struct or enum must be local to the current crate. So, for example, unsafe impl Send for Rc<Foo> is not allowed.

E0322

The Sized trait is a special trait built-in to the compiler for types with a constant size known at compile-time. This trait is automatically implemented for types as needed by the compiler, and it is currently disallowed to explicitly implement it for a type.

E0323

An associated const was implemented when another trait item was expected. Erroneous code example:

#![feature(associated_consts)]

trait Foo {
    type N;
}

struct Bar;

impl Foo for Bar {
    const N : u32 = 0;
    // error: item `N` is an associated const, which doesn't match its
    //        trait `<Bar as Foo>`
}

Please verify that the associated const wasn't misspelled and the correct trait was implemented. Example:

struct Bar;

trait Foo {
    type N;
}

impl Foo for Bar {
    type N = u32; // ok!
}

Or:

#![feature(associated_consts)]

struct Bar;

trait Foo {
    const N : u32;
}

impl Foo for Bar {
    const N : u32 = 0; // ok!
}

E0324

A method was implemented when another trait item was expected. Erroneous code example:

#![feature(associated_consts)]

struct Bar;

trait Foo {
    const N : u32;

    fn M();
}

impl Foo for Bar {
    fn N() {}
    // error: item `N` is an associated method, which doesn't match its
    //        trait `<Bar as Foo>`
}

To fix this error, please verify that the method name wasn't misspelled and verify that you are indeed implementing the correct trait items. Example:

#![feature(associated_consts)]

struct Bar;

trait Foo {
    const N : u32;

    fn M();
}

impl Foo for Bar {
    const N : u32 = 0;

    fn M() {} // ok!
}

E0325

An associated type was implemented when another trait item was expected. Erroneous code example:

#![feature(associated_consts)]

struct Bar;

trait Foo {
    const N : u32;
}

impl Foo for Bar {
    type N = u32;
    // error: item `N` is an associated type, which doesn't match its
    //        trait `<Bar as Foo>`
}

Please verify that the associated type name wasn't misspelled and your implementation corresponds to the trait definition. Example:

struct Bar;

trait Foo {
    type N;
}

impl Foo for Bar {
    type N = u32; // ok!
}

Or:

#![feature(associated_consts)]

struct Bar;

trait Foo {
    const N : u32;
}

impl Foo for Bar {
    const N : u32 = 0; // ok!
}

E0326

The types of any associated constants in a trait implementation must match the types in the trait definition. This error indicates that there was a mismatch.

Here's an example of this error:

#![feature(associated_consts)]

trait Foo {
    const BAR: bool;
}

struct Bar;

impl Foo for Bar {
    const BAR: u32 = 5; // error, expected bool, found u32
}

E0328

The Unsize trait should not be implemented directly. All implementations of Unsize are provided automatically by the compiler.

Erroneous code example:

#![feature(unsize)]

use std::marker::Unsize;

pub struct MyType;

impl<T> Unsize<T> for MyType {}

If you are defining your own smart pointer type and would like to enable conversion from a sized to an unsized type with the DST coercion system, use CoerceUnsized instead.

#![feature(coerce_unsized)]

use std::ops::CoerceUnsized;

pub struct MyType<T: ?Sized> {
    field_with_unsized_type: T,
}

impl<T, U> CoerceUnsized<MyType<U>> for MyType<T>
    where T: CoerceUnsized<U> {}

E0329

An attempt was made to access an associated constant through either a generic type parameter or Self. This is not supported yet. An example causing this error is shown below:

#![feature(associated_consts)]

trait Foo {
    const BAR: f64;
}

struct MyStruct;

impl Foo for MyStruct {
    const BAR: f64 = 0f64;
}

fn get_bar_bad<F: Foo>(t: F) -> f64 {
    F::BAR
}

Currently, the value of BAR for a particular type can only be accessed through a concrete type, as shown below:

#![feature(associated_consts)]

trait Foo {
    const BAR: f64;
}

struct MyStruct;

fn get_bar_good() -> f64 {
    <MyStruct as Foo>::BAR
}

E0364

Private items cannot be publicly re-exported. This error indicates that you attempted to pub use a type or value that was not itself public.

Erroneous code example:

mod foo {
    const X: u32 = 1;
}

pub use foo::X;

fn main() {}

The solution to this problem is to ensure that the items that you are re-exporting are themselves marked with pub:

mod foo {
    pub const X: u32 = 1;
}

pub use foo::X;

fn main() {}

See the 'Use Declarations' section of the reference for more information on this topic:

https://doc.rust-lang.org/reference.html#use-declarations

E0365

Private modules cannot be publicly re-exported. This error indicates that you attempted to pub use a module that was not itself public.

Erroneous code example:

mod foo {
    pub const X: u32 = 1;
}

pub use foo as foo2;

fn main() {}

The solution to this problem is to ensure that the module that you are re-exporting is itself marked with pub:

pub mod foo {
    pub const X: u32 = 1;
}

pub use foo as foo2;

fn main() {}

See the 'Use Declarations' section of the reference for more information on this topic:

https://doc.rust-lang.org/reference.html#use-declarations

E0366

An attempt was made to implement Drop on a concrete specialization of a generic type. An example is shown below:

struct Foo<T> {
    t: T
}

impl Drop for Foo<u32> {
    fn drop(&mut self) {}
}

This code is not legal: it is not possible to specialize Drop to a subset of implementations of a generic type. One workaround for this is to wrap the generic type, as shown below:

struct Foo<T> {
    t: T
}

struct Bar {
    t: Foo<u32>
}

impl Drop for Bar {
    fn drop(&mut self) {}
}

E0367

An attempt was made to implement Drop on a specialization of a generic type. An example is shown below:

trait Foo{}

struct MyStruct<T> {
    t: T
}

impl<T: Foo> Drop for MyStruct<T> {
    fn drop(&mut self) {}
}

This code is not legal: it is not possible to specialize Drop to a subset of implementations of a generic type. In order for this code to work, MyStruct must also require that T implements Foo. Alternatively, another option is to wrap the generic type in another that specializes appropriately:

trait Foo{}

struct MyStruct<T> {
    t: T
}

struct MyStructWrapper<T: Foo> {
    t: MyStruct<T>
}

impl <T: Foo> Drop for MyStructWrapper<T> {
    fn drop(&mut self) {}
}

E0368

This error indicates that a binary assignment operator like += or ^= was applied to a type that doesn't support it. For example:

let mut x = 12f32; // error: binary operation `<<` cannot be applied to
                   //        type `f32`

x <<= 2;

To fix this error, please check that this type implements this binary operation. Example:

let mut x = 12u32; // the `u32` type does implement the `ShlAssign` trait

x <<= 2; // ok!

It is also possible to overload most operators for your own type by implementing the [OP]Assign traits from std::ops.

Another problem you might be facing is this: suppose you've overloaded the + operator for some type Foo by implementing the std::ops::Add trait for Foo, but you find that using += does not work, as in this example:

use std::ops::Add;

struct Foo(u32);

impl Add for Foo {
    type Output = Foo;

    fn add(self, rhs: Foo) -> Foo {
        Foo(self.0 + rhs.0)
    }
}

fn main() {
    let mut x: Foo = Foo(5);
    x += Foo(7); // error, `+= cannot be applied to the type `Foo`
}

This is because AddAssign is not automatically implemented, so you need to manually implement it for your type.

E0369

A binary operation was attempted on a type which doesn't support it. Erroneous code example:

let x = 12f32; // error: binary operation `<<` cannot be applied to
               //        type `f32`

x << 2;

To fix this error, please check that this type implements this binary operation. Example:

let x = 12u32; // the `u32` type does implement it:
               // https://doc.rust-lang.org/stable/std/ops/trait.Shl.html

x << 2; // ok!

It is also possible to overload most operators for your own type by implementing traits from std::ops.

E0370

The maximum value of an enum was reached, so it cannot be automatically set in the next enum value. Erroneous code example:

#[deny(overflowing_literals)]
enum Foo {
    X = 0x7fffffffffffffff,
    Y, // error: enum discriminant overflowed on value after
       //        9223372036854775807: i64; set explicitly via
       //        Y = -9223372036854775808 if that is desired outcome
}

To fix this, please set manually the next enum value or put the enum variant with the maximum value at the end of the enum. Examples:

enum Foo {
    X = 0x7fffffffffffffff,
    Y = 0, // ok!
}

Or:

enum Foo {
    Y = 0, // ok!
    X = 0x7fffffffffffffff,
}

E0371

When Trait2 is a subtrait of Trait1 (for example, when Trait2 has a definition like trait Trait2: Trait1 { ... }), it is not allowed to implement Trait1 for Trait2. This is because Trait2 already implements Trait1 by definition, so it is not useful to do this.

Example:

trait Foo { fn foo(&self) { } }
trait Bar: Foo { }
trait Baz: Bar { }

impl Bar for Baz { } // error, `Baz` implements `Bar` by definition
impl Foo for Baz { } // error, `Baz` implements `Bar` which implements `Foo`
impl Baz for Baz { } // error, `Baz` (trivially) implements `Baz`
impl Baz for Bar { } // Note: This is OK

E0373

This error occurs when an attempt is made to use data captured by a closure, when that data may no longer exist. It's most commonly seen when attempting to return a closure:

fn foo() -> Box<Fn(u32) -> u32> {
    let x = 0u32;
    Box::new(|y| x + y)
}

Notice that x is stack-allocated by foo(). By default, Rust captures closed-over data by reference. This means that once foo() returns, x no longer exists. An attempt to access x within the closure would thus be unsafe.

Another situation where this might be encountered is when spawning threads:

fn foo() {
    let x = 0u32;
    let y = 1u32;

    let thr = std::thread::spawn(|| {
        x + y
    });
}

Since our new thread runs in parallel, the stack frame containing x and y may well have disappeared by the time we try to use them. Even if we call thr.join() within foo (which blocks until thr has completed, ensuring the stack frame won't disappear), we will not succeed: the compiler cannot prove that this behaviour is safe, and so won't let us do it.

The solution to this problem is usually to switch to using a move closure. This approach moves (or copies, where possible) data into the closure, rather than taking references to it. For example:

fn foo() -> Box<Fn(u32) -> u32> {
    let x = 0u32;
    Box::new(move |y| x + y)
}

Now that the closure has its own copy of the data, there's no need to worry about safety.

E0374

A struct without a field containing an unsized type cannot implement CoerceUnsized. An unsized type is any type that the compiler doesn't know the length or alignment of at compile time. Any struct containing an unsized type is also unsized.

Example of erroneous code:

#![feature(coerce_unsized)]
use std::ops::CoerceUnsized;

struct Foo<T: ?Sized> {
    a: i32,
}

// error: Struct `Foo` has no unsized fields that need `CoerceUnsized`.
impl<T, U> CoerceUnsized<Foo<U>> for Foo<T>
    where T: CoerceUnsized<U> {}

CoerceUnsized is used to coerce one struct containing an unsized type into another struct containing a different unsized type. If the struct doesn't have any fields of unsized types then you don't need explicit coercion to get the types you want. To fix this you can either not try to implement CoerceUnsized or you can add a field that is unsized to the struct.

Example:

#![feature(coerce_unsized)]
use std::ops::CoerceUnsized;

// We don't need to impl `CoerceUnsized` here.
struct Foo {
    a: i32,
}

// We add the unsized type field to the struct.
struct Bar<T: ?Sized> {
    a: i32,
    b: T,
}

// The struct has an unsized field so we can implement
// `CoerceUnsized` for it.
impl<T, U> CoerceUnsized<Bar<U>> for Bar<T>
    where T: CoerceUnsized<U> {}

Note that CoerceUnsized is mainly used by smart pointers like Box, Rc and Arc to be able to mark that they can coerce unsized types that they are pointing at.

E0375

A struct with more than one field containing an unsized type cannot implement CoerceUnsized. This only occurs when you are trying to coerce one of the types in your struct to another type in the struct. In this case we try to impl CoerceUnsized from T to U which are both types that the struct takes. An unsized type is any type that the compiler doesn't know the length or alignment of at compile time. Any struct containing an unsized type is also unsized.

Example of erroneous code:

#![feature(coerce_unsized)]
use std::ops::CoerceUnsized;

struct Foo<T: ?Sized, U: ?Sized> {
    a: i32,
    b: T,
    c: U,
}

// error: Struct `Foo` has more than one unsized field.
impl<T, U> CoerceUnsized<Foo<U, T>> for Foo<T, U> {}

CoerceUnsized only allows for coercion from a structure with a single unsized type field to another struct with a single unsized type field. In fact Rust only allows for a struct to have one unsized type in a struct and that unsized type must be the last field in the struct. So having two unsized types in a single struct is not allowed by the compiler. To fix this use only one field containing an unsized type in the struct and then use multiple structs to manage each unsized type field you need.

Example:

#![feature(coerce_unsized)]
use std::ops::CoerceUnsized;

struct Foo<T: ?Sized> {
    a: i32,
    b: T,
}

impl <T, U> CoerceUnsized<Foo<U>> for Foo<T>
    where T: CoerceUnsized<U> {}

fn coerce_foo<T: CoerceUnsized<U>, U>(t: T) -> Foo<U> {
    Foo { a: 12i32, b: t } // we use coercion to get the `Foo<U>` type we need
}

E0376

The type you are trying to impl CoerceUnsized for is not a struct. CoerceUnsized can only be implemented for a struct. Unsized types are already able to be coerced without an implementation of CoerceUnsized whereas a struct containing an unsized type needs to know the unsized type field it's containing is able to be coerced. An unsized type is any type that the compiler doesn't know the length or alignment of at compile time. Any struct containing an unsized type is also unsized.

Example of erroneous code:

#![feature(coerce_unsized)]
use std::ops::CoerceUnsized;

struct Foo<T: ?Sized> {
    a: T,
}

// error: The type `U` is not a struct
impl<T, U> CoerceUnsized<U> for Foo<T> {}

The CoerceUnsized trait takes a struct type. Make sure the type you are providing to CoerceUnsized is a struct with only the last field containing an unsized type.

Example:

#![feature(coerce_unsized)]
use std::ops::CoerceUnsized;

struct Foo<T> {
    a: T,
}

// The `Foo<U>` is a struct so `CoerceUnsized` can be implemented
impl<T, U> CoerceUnsized<Foo<U>> for Foo<T> where T: CoerceUnsized<U> {}

Note that in Rust, structs can only contain an unsized type if the field containing the unsized type is the last and only unsized type field in the struct.

E0380

Default impls are only allowed for traits with no methods or associated items. For more information see the opt-in builtin traits RFC.

E0381

It is not allowed to use or capture an uninitialized variable. For example:

fn main() {
    let x: i32;
    let y = x; // error, use of possibly uninitialized variable
}

To fix this, ensure that any declared variables are initialized before being used. Example:

fn main() {
    let x: i32 = 0;
    let y = x; // ok!
}

E0382

This error occurs when an attempt is made to use a variable after its contents have been moved elsewhere. For 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 out of x when we set y. This is fundamental to Rust's ownership system: outside of workarounds like Rc, a value cannot be owned by more than one variable.

If we own the type, the easiest way to address this problem is to implement Copy and Clone on it, as shown below. This allows y to copy the information in x, while leaving the original version owned by x. Subsequent changes to x will not be reflected when accessing y.

#[derive(Copy, Clone)]
struct MyStruct { s: u32 }

fn main() {
    let mut x = MyStruct{ s: 5u32 };
    let y = x;
    x.s = 6;
    println!("{}", x.s);
}

Alternatively, if we don't control the struct's definition, or mutable shared ownership is truly required, we can use Rc and RefCell:

use std::cell::RefCell;
use std::rc::Rc;

struct MyStruct { s: u32 }

fn main() {
    let mut x = Rc::new(RefCell::new(MyStruct{ s: 5u32 }));
    let y = x.clone();
    x.borrow_mut().s = 6;
    println!("{}", x.borrow().s);
}

With this approach, x and y share ownership of the data via the Rc (reference count type). RefCell essentially performs runtime borrow checking: ensuring that at most one writer or multiple readers can access the data at any one time.

If you wish to learn more about ownership in Rust, start with the chapter in the Book:

https://doc.rust-lang.org/book/ownership.html

E0383

This error occurs when an attempt is made to partially reinitialize a structure that is currently uninitialized.

For example, this can happen when a drop has taken place:

struct Foo {
    a: u32,
}

let mut x = Foo { a: 1 };
drop(x); // `x` is now uninitialized
x.a = 2; // error, partial reinitialization of uninitialized structure `t`

This error can be fixed by fully reinitializing the structure in question:

struct Foo {
    a: u32,
}

let mut x = Foo { a: 1 };
drop(x);
x = Foo { a: 2 };

E0384

This error occurs when an attempt is made to reassign an immutable variable. For example:

fn main() {
    let x = 3;
    x = 5; // error, reassignment of immutable variable
}

By default, variables in Rust are immutable. To fix this error, add the keyword mut after the keyword let when declaring the variable. For example:

fn main() {
    let mut x = 3;
    x = 5;
}

E0387

This error occurs when an attempt is made to mutate or mutably reference data that a closure has captured immutably. Examples of this error are shown below:

// Accepts a function or a closure that captures its environment immutably.
// Closures passed to foo will not be able to mutate their closed-over state.
fn foo<F: Fn()>(f: F) { }

// Attempts to mutate closed-over data. Error message reads:
// `cannot assign to data in a captured outer variable...`
fn mutable() {
    let mut x = 0u32;
    foo(|| x = 2);
}

// Attempts to take a mutable reference to closed-over data.  Error message
// reads: `cannot borrow data mutably in a captured outer variable...`
fn mut_addr() {
    let mut x = 0u32;
    foo(|| { let y = &mut x; });
}

The problem here is that foo is defined as accepting a parameter of type Fn. Closures passed into foo will thus be inferred to be of type Fn, meaning that they capture their context immutably.

If the definition of foo is under your control, the simplest solution is to capture the data mutably. This can be done by defining foo to take FnMut rather than Fn:

fn foo<F: FnMut()>(f: F) { }

Alternatively, we can consider using the Cell and RefCell types to achieve interior mutability through a shared reference. Our example's mutable function could be redefined as below:

use std::cell::Cell;

fn foo<F: Fn()>(f: F) { }

fn mutable() {
    let x = Cell::new(0u32);
    foo(|| x.set(2));
}

You can read more about cell types in the API documentation:

https://doc.rust-lang.org/std/cell/

E0388

E0388 was removed and is no longer issued.

E0389

An attempt was made to mutate data using a non-mutable reference. This commonly occurs when attempting to assign to a non-mutable reference of a mutable reference (&(&mut T)).

Example of erroneous code:

struct FancyNum {
    num: u8,
}

fn main() {
    let mut fancy = FancyNum{ num: 5 };
    let fancy_ref = &(&mut fancy);
    fancy_ref.num = 6; // error: cannot assign to data in a `&` reference
    println!("{}", fancy_ref.num);
}

Here, &mut fancy is mutable, but &(&mut fancy) is not. Creating an immutable reference to a value borrows it immutably. There can be multiple references of type &(&mut T) that point to the same value, so they must be immutable to prevent multiple mutable references to the same value.

To fix this, either remove the outer reference:

struct FancyNum {
    num: u8,
}

fn main() {
    let mut fancy = FancyNum{ num: 5 };

    let fancy_ref = &mut fancy;
    // `fancy_ref` is now &mut FancyNum, rather than &(&mut FancyNum)

    fancy_ref.num = 6; // No error!

    println!("{}", fancy_ref.num);
}

Or make the outer reference mutable:

struct FancyNum {
    num: u8
}

fn main() {
    let mut fancy = FancyNum{ num: 5 };

    let fancy_ref = &mut (&mut fancy);
    // `fancy_ref` is now &mut(&mut FancyNum), rather than &(&mut FancyNum)

    fancy_ref.num = 6; // No error!

    println!("{}", fancy_ref.num);
}

E0390

You tried to implement methods for a primitive type. Erroneous code example:

struct Foo {
    x: i32
}

impl *mut Foo {}
// error: only a single inherent implementation marked with
//        `#[lang = "mut_ptr"]` is allowed for the `*mut T` primitive

This isn't allowed, but using a trait to implement a method is a good solution. Example:

struct Foo {
    x: i32
}

trait Bar {
    fn bar();
}

impl Bar for *mut Foo {
    fn bar() {} // ok!
}

E0391

This error indicates that some types or traits depend on each other and therefore cannot be constructed.

The following example contains a circular dependency between two traits:

trait FirstTrait : SecondTrait {

}

trait SecondTrait : FirstTrait {

}

E0392

This error indicates that a type or lifetime parameter has been declared but not actually used. Here is an example that demonstrates the error:

enum Foo<T> {
    Bar,
}

If the type parameter was included by mistake, this error can be fixed by simply removing the type parameter, as shown below:

enum Foo {
    Bar,
}

Alternatively, if the type parameter was intentionally inserted, it must be used. A simple fix is shown below:

enum Foo<T> {
    Bar(T),
}

This error may also commonly be found when working with unsafe code. For example, when using raw pointers one may wish to specify the lifetime for which the pointed-at data is valid. An initial attempt (below) causes this error:

struct Foo<'a, T> {
    x: *const T,
}

We want to express the constraint that Foo should not outlive 'a, because the data pointed to by T is only valid for that lifetime. The problem is that there are no actual uses of 'a. It's possible to work around this by adding a PhantomData type to the struct, using it to tell the compiler to act as if the struct contained a borrowed reference &'a T:

use std::marker::PhantomData;

struct Foo<'a, T: 'a> {
    x: *const T,
    phantom: PhantomData<&'a T>
}

PhantomData can also be used to express information about unused type parameters. You can read more about it in the API documentation:

https://doc.rust-lang.org/std/marker/struct.PhantomData.html

E0393

A type parameter which references Self in its default value was not specified. Example of erroneous code:

trait A<T=Self> {}

fn together_we_will_rule_the_galaxy(son: &A) {}
// error: the type parameter `T` must be explicitly specified in an
//        object type because its default value `Self` references the
//        type `Self`

A trait object is defined over a single, fully-defined trait. With a regular default parameter, this parameter can just be substituted in. However, if the default parameter is Self, the trait changes for each concrete type; i.e. i32 will be expected to implement A<i32>, bool will be expected to implement A<bool>, etc... These types will not share an implementation of a fully-defined trait; instead they share implementations of a trait with different parameters substituted in for each implementation. This is irreconcilable with what we need to make a trait object work, and is thus disallowed. Making the trait concrete by explicitly specifying the value of the defaulted parameter will fix this issue. Fixed example:

trait A<T=Self> {}

fn together_we_will_rule_the_galaxy(son: &A<i32>) {} // Ok!

E0398

In Rust 1.3, the default object lifetime bounds are expected to change, as described in RFC 1156. You are getting a warning because the compiler thinks it is possible that this change will cause a compilation error in your code. It is possible, though unlikely, that this is a false alarm.

The heart of the change is that where &'a Box<SomeTrait> used to default to &'a Box<SomeTrait+'a>, it now defaults to &'a Box<SomeTrait+'static> (here, SomeTrait is the name of some trait type). Note that the only types which are affected are references to boxes, like &Box<SomeTrait> or &[Box<SomeTrait>]. More common types like &SomeTrait or Box<SomeTrait> are unaffected.

To silence this warning, edit your code to use an explicit bound. Most of the time, this means that you will want to change the signature of a function that you are calling. For example, if the error is reported on a call like foo(x), and foo is defined as follows:

fn foo(arg: &Box<SomeTrait>) { ... }

You might change it to:

fn foo<'a>(arg: &Box<SomeTrait+'a>) { ... }

This explicitly states that you expect the trait object SomeTrait to contain references (with a maximum lifetime of 'a).

E0399

You implemented a trait, overriding one or more of its associated types but did not reimplement its default methods.

Example of erroneous code:

#![feature(associated_type_defaults)]

pub trait Foo {
    type Assoc = u8;
    fn bar(&self) {}
}

impl Foo for i32 {
    // error - the following trait items need to be reimplemented as
    //         `Assoc` was overridden: `bar`
    type Assoc = i32;
}

To fix this, add an implementation for each default method from the trait:

#![feature(associated_type_defaults)]

pub trait Foo {
    type Assoc = u8;
    fn bar(&self) {}
}

impl Foo for i32 {
    type Assoc = i32;
    fn bar(&self) {} // ok!
}

E0401

Inner items do not inherit type parameters from the functions they are embedded in.

Erroneous code example:

fn foo<T>(x: T) {
    fn bar(y: T) { // T is defined in the "outer" function
        // ..
    }
    bar(x);
}

Nor will this:

fn foo<T>(x: T) {
    type MaybeT = Option<T>;
    // ...
}

Or this:

fn foo<T>(x: T) {
    struct Foo {
        x: T,
    }
    // ...
}

Items inside functions are basically just like top-level items, except that they can only be used from the function they are in.

There are a couple of solutions for this.

If the item is a function, you may use a closure:

fn foo<T>(x: T) {
    let bar = |y: T| { // explicit type annotation may not be necessary
        // ..
    };
    bar(x);
}

For a generic item, you can copy over the parameters:

fn foo<T>(x: T) {
    fn bar<T>(y: T) {
        // ..
    }
    bar(x);
}
fn foo<T>(x: T) {
    type MaybeT<T> = Option<T>;
}

Be sure to copy over any bounds as well:

fn foo<T: Copy>(x: T) {
    fn bar<T: Copy>(y: T) {
        // ..
    }
    bar(x);
}
fn foo<T: Copy>(x: T) {
    struct Foo<T: Copy> {
        x: T,
    }
}

This may require additional type hints in the function body.

In case the item is a function inside an impl, defining a private helper function might be easier:

impl<T> Foo<T> {
    pub fn foo(&self, x: T) {
        self.bar(x);
    }

    fn bar(&self, y: T) {
        // ..
    }
}

For default impls in traits, the private helper solution won't work, however closures or copying the parameters should still work.

E0403

Some type parameters have the same name.

Erroneous code example:

fn foo<T, T>(s: T, u: T) {} // error: the name `T` is already used for a type
                            //        parameter in this type parameter list

Please verify that none of the type parameterss are misspelled, and rename any clashing parameters. Example:

fn foo<T, Y>(s: T, u: Y) {} // ok!

E0404

You tried to implement something which was not a trait on an object.

Erroneous code example:

struct Foo;
struct Bar;

impl Foo for Bar {} // error: `Foo` is not a trait

Please verify that you didn't misspell the trait's name or otherwise use the wrong identifier. Example:

trait Foo {
    // some functions
}
struct Bar;

impl Foo for Bar { // ok!
    // functions implementation
}

E0405

The code refers to a trait that is not in scope.

Erroneous code example:

struct Foo;

impl SomeTrait for Foo {} // error: trait `SomeTrait` is not in scope

Please verify that the name of the trait wasn't misspelled and ensure that it was imported. Example:

// solution 1:
use some_file::SomeTrait;

// solution 2:
trait SomeTrait {
    // some functions
}

struct Foo;

impl SomeTrait for Foo { // ok!
    // implements functions
}

E0407

A definition of a method not in the implemented trait was given in a trait implementation.

Erroneous code example:

trait Foo {
    fn a();
}

struct Bar;

impl Foo for Bar {
    fn a() {}
    fn b() {} // error: method `b` is not a member of trait `Foo`
}

Please verify you didn't misspell the method name and you used the correct trait. First example:

trait Foo {
    fn a();
    fn b();
}

struct Bar;

impl Foo for Bar {
    fn a() {}
    fn b() {} // ok!
}

Second example:

trait Foo {
    fn a();
}

struct Bar;

impl Foo for Bar {
    fn a() {}
}

impl Bar {
    fn b() {}
}

E0408

An "or" pattern was used where the variable bindings are not consistently bound across patterns.

Erroneous code example:

match x {
    Some(y) | None => { /* use y */ } // error: variable `y` from pattern #1 is
                                      //        not bound in pattern #2
    _ => ()
}

Here, y is bound to the contents of the Some and can be used within the block corresponding to the match arm. However, in case x is None, we have not specified what y is, and the block will use a nonexistent variable.

To fix this error, either split into multiple match arms:

let x = Some(1);
match x {
    Some(y) => { /* use y */ }
    None => { /* ... */ }
}

or, bind the variable to a field of the same type in all sub-patterns of the or pattern:

let x = (0, 2);
match x {
    (0, y) | (y, 0) => { /* use y */}
    _ => {}
}

In this example, if x matches the pattern (0, _), the second field is set to y. If it matches (_, 0), the first field is set to y; so in all cases y is set to some value.

E0409

An "or" pattern was used where the variable bindings are not consistently bound across patterns.

Erroneous code example:

let x = (0, 2);
match x {
    (0, ref y) | (y, 0) => { /* use y */} // error: variable `y` is bound with
                                          //        different mode in pattern #2
                                          //        than in pattern #1
    _ => ()
}

Here, y is bound by-value in one case and by-reference in the other.

To fix this error, just use the same mode in both cases. Generally using ref or ref mut where not already used will fix this:

let x = (0, 2);
match x {
    (0, ref y) | (ref y, 0) => { /* use y */}
    _ => ()
}

Alternatively, split the pattern:

let x = (0, 2);
match x {
    (y, 0) => { /* use y */ }
    (0, ref y) => { /* use y */}
    _ => ()
}

E0411

The Self keyword was used outside an impl or a trait.

Erroneous code example:

<Self>::foo; // error: use of `Self` outside of an impl or trait

The Self keyword represents the current type, which explains why it can only be used inside an impl or a trait. It gives access to the associated items of a type:

trait Foo {
    type Bar;
}

trait Baz : Foo {
    fn bar() -> Self::Bar; // like this
}

However, be careful when two types have a common associated type:

trait Foo {
    type Bar;
}

trait Foo2 {
    type Bar;
}

trait Baz : Foo + Foo2 {
    fn bar() -> Self::Bar;
    // error: ambiguous associated type `Bar` in bounds of `Self`
}

This problem can be solved by specifying from which trait we want to use the Bar type:

trait Foo {
    type Bar;
}

trait Foo2 {
    type Bar;
}

trait Baz : Foo + Foo2 {
    fn bar() -> <Self as Foo>::Bar; // ok!
}

E0412

The type name used is not in scope.

Erroneous code examples:

impl Something {} // error: type name `Something` is not in scope

// or:

trait Foo {
    fn bar(N); // error: type name `N` is not in scope
}

// or:

fn foo(x: T) {} // type name `T` is not in scope

To fix this error, please verify you didn't misspell the type name, you did declare it or imported it into the scope. Examples:

struct Something;

impl Something {} // ok!

// or:

trait Foo {
    type N;

    fn bar(Self::N); // ok!
}

// or:

fn foo<T>(x: T) {} // ok!

E0415

More than one function parameter have the same name.

Erroneous code example:

fn foo(f: i32, f: i32) {} // error: identifier `f` is bound more than
                          //        once in this parameter list

Please verify you didn't misspell parameters' name. Example:

fn foo(f: i32, g: i32) {} // ok!

E0416

An identifier is bound more than once in a pattern.

Erroneous code example:

match (1, 2) {
    (x, x) => {} // error: identifier `x` is bound more than once in the
                 //        same pattern
}

Please verify you didn't misspell identifiers' name. Example:

match (1, 2) {
    (x, y) => {} // ok!
}

Or maybe did you mean to unify? Consider using a guard:

match (A, B, C) {
    (x, x2, see) if x == x2 => { /* A and B are equal, do one thing */ }
    (y, z, see) => { /* A and B unequal; do another thing */ }
}

E0422

You are trying to use an identifier that is either undefined or not a struct. Erroneous code example:

fn main () {
    let x = Foo { x: 1, y: 2 };
}

In this case, Foo is undefined, so it inherently isn't anything, and definitely not a struct.

fn main () {
    let foo = 1;
    let x = foo { x: 1, y: 2 };
}

In this case, foo is defined, but is not a struct, so Rust can't use it as one.

E0423

A struct variant name was used like a function name.

Erroneous code example:

struct Foo { a: bool };

let f = Foo();
// error: `Foo` is a struct variant name, but this expression uses
//        it like a function name

Please verify you didn't misspell the name of what you actually wanted to use here. Example:

fn Foo() -> u32 { 0 }

let f = Foo(); // ok!

E0424

The self keyword was used in a static method.

Erroneous code example:

struct Foo;

impl Foo {
    fn bar(self) {}

    fn foo() {
        self.bar(); // error: `self` is not available in a static method.
    }
}

Please check if the method's argument list should have contained self, &self, or &mut self (in case you didn't want to create a static method), and add it if so. Example:

struct Foo;

impl Foo {
    fn bar(self) {}

    fn foo(self) {
        self.bar(); // ok!
    }
}

E0425

An unresolved name was used.

Erroneous code examples:

something_that_doesnt_exist::foo;
// error: unresolved name `something_that_doesnt_exist::foo`

// or:

trait Foo {
    fn bar() {
        Self; // error: unresolved name `Self`
    }
}

// or:

let x = unknown_variable;  // error: unresolved name `unknown_variable`

Please verify that the name wasn't misspelled and ensure that the identifier being referred to is valid for the given situation. Example:

enum something_that_does_exist {
    Foo,
}

Or:

mod something_that_does_exist {
    pub static foo : i32 = 0i32;
}

something_that_does_exist::foo; // ok!

Or:

let unknown_variable = 12u32;
let x = unknown_variable; // ok!

If the item is not defined in the current module, it must be imported using a use statement, like so:

use foo::bar;
bar();

If the item you are importing is not defined in some super-module of the current module, then it must also be declared as public (e.g., pub fn).

E0426

An undeclared label was used.

Erroneous code example:

loop {
    break 'a; // error: use of undeclared label `'a`
}

Please verify you spelt or declare the label correctly. Example:

'a: loop {
    break 'a; // ok!
}

E0428

A type or module has been defined more than once.

Erroneous code example:

struct Bar;
struct Bar; // error: duplicate definition of value `Bar`

Please verify you didn't misspell the type/module's name or remove/rename the duplicated one. Example:

struct Bar;
struct Bar2; // ok!

E0429

The self keyword cannot appear alone as the last segment in a use declaration.

Erroneous code example:

use std::fmt::self; // error: `self` imports are only allowed within a { } list

To use a namespace itself in addition to some of its members, self may appear as part of a brace-enclosed list of imports:

use std::fmt::{self, Debug};

If you only want to import the namespace, do so directly:

use std::fmt;

E0430

The self import appears more than once in the list.

Erroneous code example:

use something::{self, self}; // error: `self` import can only appear once in
                             //        the list

Please verify you didn't misspell the import name or remove the duplicated self import. Example:

use something::self; // ok!

E0431

An invalid self import was made.

Erroneous code example:

use {self}; // error: `self` import can only appear in an import list with a
            //        non-empty prefix

You cannot import the current module into itself, please remove this import or verify you didn't misspell it.

E0432

An import was unresolved.

Erroneous code example:

use something::Foo; // error: unresolved import `something::Foo`.

Paths in use statements are relative to the crate root. To import items relative to the current and parent modules, use the self:: and super:: prefixes, respectively. Also verify that you didn't misspell the import name and that the import exists in the module from where you tried to import it. Example:

use self::something::Foo; // ok!

mod something {
    pub struct Foo;
}

Or, if you tried to use a module from an external crate, you may have missed the extern crate declaration (which is usually placed in the crate root):

extern crate homura; // Required to use the `homura` crate

use homura::Madoka;

E0433

An undeclared type or module was used.

Erroneous code example:

let map = HashMap::new();
// error: failed to resolve. Use of undeclared type or module `HashMap`

Please verify you didn't misspell the type/module's name or that you didn't forgot to import it:

use std::collections::HashMap; // HashMap has been imported.
let map: HashMap<u32, u32> = HashMap::new(); // So it can be used!

E0434

This error indicates that a variable usage inside an inner function is invalid because the variable comes from a dynamic environment. Inner functions do not have access to their containing environment.

Erroneous code example:

fn foo() {
    let y = 5;
    fn bar() -> u32 {
        y // error: can't capture dynamic environment in a fn item; use the
          //        || { ... } closure form instead.
    }
}

Functions do not capture local variables. To fix this error, you can replace the function with a closure:

fn foo() {
    let y = 5;
    let bar = || {
        y
    };
}

or replace the captured variable with a constant or a static item:

fn foo() {
    static mut X: u32 = 4;
    const Y: u32 = 5;
    fn bar() -> u32 {
        unsafe {
            X = 3;
        }
        Y
    }
}

E0435

A non-constant value was used to initialise a constant.

Erroneous code example:

let foo = 42u32;
const FOO : u32 = foo; // error: attempt to use a non-constant value in a
                       //        constant

To fix this error, please replace the value with a constant. Example:

const FOO : u32 = 42u32; // ok!

Or:

const OTHER_FOO : u32 = 42u32;
const FOO : u32 = OTHER_FOO; // ok!

E0437

Trait implementations can only implement associated types that are members of the trait in question. This error indicates that you attempted to implement an associated type whose name does not match the name of any associated type in the trait.

Erroneous code example:

trait Foo {}

impl Foo for i32 {
    type Bar = bool;
}

The solution to this problem is to remove the extraneous associated type:

trait Foo {}

impl Foo for i32 {}

E0438

Trait implementations can only implement associated constants that are members of the trait in question. This error indicates that you attempted to implement an associated constant whose name does not match the name of any associated constant in the trait.

Erroneous code example:

#![feature(associated_consts)]

trait Foo {}

impl Foo for i32 {
    const BAR: bool = true;
}

The solution to this problem is to remove the extraneous associated constant:

trait Foo {}

impl Foo for i32 {}

E0439

The length of the platform-intrinsic function simd_shuffle wasn't specified. Erroneous code example:

#![feature(platform_intrinsics)]

extern "platform-intrinsic" {
    fn simd_shuffle<A,B>(a: A, b: A, c: [u32; 8]) -> B;
    // error: invalid `simd_shuffle`, needs length: `simd_shuffle`
}

The simd_shuffle function needs the length of the array passed as last parameter in its name. Example:

#![feature(platform_intrinsics)]

extern "platform-intrinsic" {
    fn simd_shuffle8<A,B>(a: A, b: A, c: [u32; 8]) -> B;
}

E0440

A platform-specific intrinsic function has the wrong number of type parameters. Erroneous code example:

#![feature(repr_simd)]
#![feature(platform_intrinsics)]

#[repr(simd)]
struct f64x2(f64, f64);

extern "platform-intrinsic" {
    fn x86_mm_movemask_pd<T>(x: f64x2) -> i32;
    // error: platform-specific intrinsic has wrong number of type
    //        parameters
}

Please refer to the function declaration to see if it corresponds with yours. Example:

#![feature(repr_simd)]
#![feature(platform_intrinsics)]

#[repr(simd)]
struct f64x2(f64, f64);

extern "platform-intrinsic" {
    fn x86_mm_movemask_pd(x: f64x2) -> i32;
}

E0441

An unknown platform-specific intrinsic function was used. Erroneous code example:

#![feature(repr_simd)]
#![feature(platform_intrinsics)]

#[repr(simd)]
struct i16x8(i16, i16, i16, i16, i16, i16, i16, i16);

extern "platform-intrinsic" {
    fn x86_mm_adds_ep16(x: i16x8, y: i16x8) -> i16x8;
    // error: unrecognized platform-specific intrinsic function
}

Please verify that the function name wasn't misspelled, and ensure that it is declared in the rust source code (in the file src/librustc_platform_intrinsics/x86.rs). Example:

#![feature(repr_simd)]
#![feature(platform_intrinsics)]

#[repr(simd)]
struct i16x8(i16, i16, i16, i16, i16, i16, i16, i16);

extern "platform-intrinsic" {
    fn x86_mm_adds_epi16(x: i16x8, y: i16x8) -> i16x8; // ok!
}

E0442

Intrinsic argument(s) and/or return value have the wrong type. Erroneous code example:

#![feature(repr_simd)]
#![feature(platform_intrinsics)]

#[repr(simd)]
struct i8x16(i8, i8, i8, i8, i8, i8, i8, i8,
             i8, i8, i8, i8, i8, i8, i8, i8);
#[repr(simd)]
struct i32x4(i32, i32, i32, i32);
#[repr(simd)]
struct i64x2(i64, i64);

extern "platform-intrinsic" {
    fn x86_mm_adds_epi16(x: i8x16, y: i32x4) -> i64x2;
    // error: intrinsic arguments/return value have wrong type
}

To fix this error, please refer to the function declaration to give it the awaited types. Example:

#![feature(repr_simd)]
#![feature(platform_intrinsics)]

#[repr(simd)]
struct i16x8(i16, i16, i16, i16, i16, i16, i16, i16);

extern "platform-intrinsic" {
    fn x86_mm_adds_epi16(x: i16x8, y: i16x8) -> i16x8; // ok!
}

E0443

Intrinsic argument(s) and/or return value have the wrong type. Erroneous code example:

#![feature(repr_simd)]
#![feature(platform_intrinsics)]

#[repr(simd)]
struct i16x8(i16, i16, i16, i16, i16, i16, i16, i16);
#[repr(simd)]
struct i64x8(i64, i64, i64, i64, i64, i64, i64, i64);

extern "platform-intrinsic" {
    fn x86_mm_adds_epi16(x: i16x8, y: i16x8) -> i64x8;
    // error: intrinsic argument/return value has wrong type
}

To fix this error, please refer to the function declaration to give it the awaited types. Example:

#![feature(repr_simd)]
#![feature(platform_intrinsics)]

#[repr(simd)]
struct i16x8(i16, i16, i16, i16, i16, i16, i16, i16);

extern "platform-intrinsic" {
    fn x86_mm_adds_epi16(x: i16x8, y: i16x8) -> i16x8; // ok!
}

E0444

A platform-specific intrinsic function has wrong number of arguments. Erroneous code example:

#![feature(repr_simd)]
#![feature(platform_intrinsics)]

#[repr(simd)]
struct f64x2(f64, f64);

extern "platform-intrinsic" {
    fn x86_mm_movemask_pd(x: f64x2, y: f64x2, z: f64x2) -> i32;
    // error: platform-specific intrinsic has invalid number of arguments
}

Please refer to the function declaration to see if it corresponds with yours. Example:

#![feature(repr_simd)]
#![feature(platform_intrinsics)]

#[repr(simd)]
struct f64x2(f64, f64);

extern "platform-intrinsic" {
    fn x86_mm_movemask_pd(x: f64x2) -> i32; // ok!
}

E0445

A private trait was used on a public type parameter bound. Erroneous code examples:

#![deny(private_in_public)]

trait Foo {
    fn dummy(&self) { }
}

pub trait Bar : Foo {} // error: private trait in public interface
pub struct Bar2<T: Foo>(pub T); // same error
pub fn foo<T: Foo> (t: T) {} // same error

To solve this error, please ensure that the trait is also public. The trait can be made inaccessible if necessary by placing it into a private inner module, but it still has to be marked with pub. Example:

pub trait Foo { // we set the Foo trait public
    fn dummy(&self) { }
}

pub trait Bar : Foo {} // ok!
pub struct Bar2<T: Foo>(pub T); // ok!
pub fn foo<T: Foo> (t: T) {} // ok!

E0446

A private type was used in a public type signature. Erroneous code example:

#![deny(private_in_public)]

mod Foo {
    struct Bar(u32);

    pub fn bar() -> Bar { // error: private type in public interface
        Bar(0)
    }
}

To solve this error, please ensure that the type is also public. The type can be made inaccessible if necessary by placing it into a private inner module, but it still has to be marked with pub. Example:

mod Foo {
    pub struct Bar(u32); // we set the Bar type public

    pub fn bar() -> Bar { // ok!
        Bar(0)
    }
}

E0447

The pub keyword was used inside a function. Erroneous code example:

fn foo() {
    pub struct Bar; // error: visibility has no effect inside functions
}

Since we cannot access items defined inside a function, the visibility of its items does not impact outer code. So using the pub keyword in this context is invalid.

E0448

The pub keyword was used inside a public enum. Erroneous code example:

pub enum Foo {
    pub Bar, // error: unnecessary `pub` visibility
}

Since the enum is already public, adding pub on one its elements is unnecessary. Example:

enum Foo {
    pub Bar, // not ok!
}

This is the correct syntax:

pub enum Foo {
    Bar, // ok!
}

E0451

A struct constructor with private fields was invoked. Erroneous code example:

mod Bar {
    pub struct Foo {
        pub a: isize,
        b: isize,
    }
}

let f = Bar::Foo{ a: 0, b: 0 }; // error: field `b` of struct `Bar::Foo`
                                //        is private

To fix this error, please ensure that all the fields of the struct are public, or implement a function for easy instantiation. Examples:

mod Bar {
    pub struct Foo {
        pub a: isize,
        pub b: isize, // we set `b` field public
    }
}

let f = Bar::Foo{ a: 0, b: 0 }; // ok!

Or:

mod Bar {
    pub struct Foo {
        pub a: isize,
        b: isize, // still private
    }

    impl Foo {
        pub fn new() -> Foo { // we create a method to instantiate `Foo`
            Foo { a: 0, b: 0 }
        }
    }
}

let f = Bar::Foo::new(); // ok!

E0452

An invalid lint attribute has been given. Erroneous code example:

#![allow(foo = "")] // error: malformed lint attribute

Lint attributes only accept a list of identifiers (where each identifier is a lint name). Ensure the attribute is of this form:

#![allow(foo)] // ok!
// or:
#![allow(foo, foo2)] // ok!

E0453

A lint check attribute was overruled by a forbid directive set as an attribute on an enclosing scope, or on the command line with the -F option.

Example of erroneous code:

#![forbid(non_snake_case)]

#[allow(non_snake_case)]
fn main() {
    let MyNumber = 2; // error: allow(non_snake_case) overruled by outer
                      //        forbid(non_snake_case)
}

The forbid lint setting, like deny, turns the corresponding compiler warning into a hard error. Unlike deny, forbid prevents itself from being overridden by inner attributes.

If you're sure you want to override the lint check, you can change forbid to deny (or use -D instead of -F if the forbid setting was given as a command-line option) to allow the inner lint check attribute:

#![deny(non_snake_case)]

#[allow(non_snake_case)]
fn main() {
    let MyNumber = 2; // ok!
}

Otherwise, edit the code to pass the lint check, and remove the overruled attribute:

#![forbid(non_snake_case)]

fn main() {
    let my_number = 2;
}

E0454

A link name was given with an empty name. Erroneous code example:

#[link(name = "")] extern {} // error: #[link(name = "")] given with empty name

The rust compiler cannot link to an external library if you don't give it its name. Example:

#[link(name = "some_lib")] extern {} // ok!

E0455

Linking with kind=framework is only supported when targeting macOS, as frameworks are specific to that operating system.

Erroneous code example:

#[link(name = "FooCoreServices", kind = "framework")] extern {}
// OS used to compile is Linux for example

To solve this error you can use conditional compilation:

#[cfg_attr(target="macos", link(name = "FooCoreServices", kind = "framework"))]
extern {}

See more: https://doc.rust-lang.org/book/conditional-compilation.html

E0458

An unknown "kind" was specified for a link attribute. Erroneous code example:

#[link(kind = "wonderful_unicorn")] extern {}
// error: unknown kind: `wonderful_unicorn`

Please specify a valid "kind" value, from one of the following:

  • static
  • dylib
  • framework

E0459

A link was used without a name parameter. Erroneous code example:

#[link(kind = "dylib")] extern {}
// error: #[link(...)] specified without `name = "foo"`

Please add the name parameter to allow the rust compiler to find the library you want. Example:

#[link(kind = "dylib", name = "some_lib")] extern {} // ok!

E0463

A plugin/crate was declared but cannot be found. Erroneous code example:

#![feature(plugin)]
#![plugin(cookie_monster)] // error: can't find crate for `cookie_monster`
extern crate cake_is_a_lie; // error: can't find crate for `cake_is_a_lie`

You need to link your code to the relevant crate in order to be able to use it (through Cargo or the -L option of rustc example). Plugins are crates as well, and you link to them the same way.

E0466

Macro import declarations were malformed.

Erroneous code examples:

#[macro_use(a_macro(another_macro))] // error: invalid import declaration
extern crate core as some_crate;

#[macro_use(i_want = "some_macros")] // error: invalid import declaration
extern crate core as another_crate;

This is a syntax error at the level of attribute declarations. The proper syntax for macro imports is the following:

// In some_crate:
#[macro_export]
macro_rules! get_tacos {
    ...
}

#[macro_export]
macro_rules! get_pimientos {
    ...
}

// In your crate:
#[macro_use(get_tacos, get_pimientos)] // It imports `get_tacos` and
extern crate some_crate;               // `get_pimientos` macros from some_crate

If you would like to import all exported macros, write macro_use with no arguments.

E0467

Macro reexport declarations were empty or malformed.

Erroneous code examples:

#[macro_reexport]                    // error: no macros listed for export
extern crate core as macros_for_good;

#[macro_reexport(fun_macro = "foo")] // error: not a macro identifier
extern crate core as other_macros_for_good;

This is a syntax error at the level of attribute declarations.

Currently, macro_reexport requires at least one macro name to be listed. Unlike macro_use, listing no names does not reexport all macros from the given crate.

Decide which macros you would like to export and list them properly.

These are proper reexport declarations:

#[macro_reexport(some_macro, another_macro)]
extern crate macros_for_good;

E0468

A non-root module attempts to import macros from another crate.

Example of erroneous code:

mod foo {
    #[macro_use(helpful_macro)] // error: must be at crate root to import
    extern crate core;          //        macros from another crate
    helpful_macro!(...);
}

Only extern crate imports at the crate root level are allowed to import macros.

Either move the macro import to crate root or do without the foreign macros. This will work:

#[macro_use(helpful_macro)]
extern crate some_crate;

mod foo {
    helpful_macro!(...)
}

E0469

A macro listed for import was not found.

Erroneous code example:

#[macro_use(drink, be_merry)] // error: imported macro not found
extern crate collections;

fn main() {
    // ...
}

Either the listed macro is not contained in the imported crate, or it is not exported from the given crate.

This could be caused by a typo. Did you misspell the macro's name?

Double-check the names of the macros listed for import, and that the crate in question exports them.

A working version would be:

// In some_crate crate:
#[macro_export]
macro_rules! eat {
    ...
}

#[macro_export]
macro_rules! drink {
    ...
}

// In your crate:
#[macro_use(eat, drink)]
extern crate some_crate; //ok!

E0470

A macro listed for reexport was not found.

Erroneous code example:

#[macro_reexport(drink, be_merry)]
extern crate collections;

fn main() {
    // ...
}

Either the listed macro is not contained in the imported crate, or it is not exported from the given crate.

This could be caused by a typo. Did you misspell the macro's name?

Double-check the names of the macros listed for reexport, and that the crate in question exports them.

A working version:

// In some_crate crate:
#[macro_export]
macro_rules! eat {
    ...
}

#[macro_export]
macro_rules! drink {
    ...
}

// In your_crate:
#[macro_reexport(eat, drink)]
extern crate some_crate;

E0478

A lifetime bound was not satisfied.

Erroneous code example:

// Check that the explicit lifetime bound (`'SnowWhite`, in this example) must
// outlive all the superbounds from the trait (`'kiss`, in this example).

trait Wedding<'t>: 't { }

struct Prince<'kiss, 'SnowWhite> {
    child: Box<Wedding<'kiss> + 'SnowWhite>,
    // error: lifetime bound not satisfied
}

In this example, the 'SnowWhite lifetime is supposed to outlive the 'kiss lifetime but the declaration of the Prince struct doesn't enforce it. To fix this issue, you need to specify it:

trait Wedding<'t>: 't { }

struct Prince<'kiss, 'SnowWhite: 'kiss> { // You say here that 'kiss must live
                                          // longer than 'SnowWhite.
    child: Box<Wedding<'kiss> + 'SnowWhite>, // And now it's all good!
}

E0491

A reference has a longer lifetime than the data it references.

Erroneous code example:

// struct containing a reference requires a lifetime parameter,
// because the data the reference points to must outlive the struct (see E0106)
struct Struct<'a> {
    ref_i32: &'a i32,
}

// However, a nested struct like this, the signature itself does not tell
// whether 'a outlives 'b or the other way around.
// So it could be possible that 'b of reference outlives 'a of the data.
struct Nested<'a, 'b> {
    ref_struct: &'b Struct<'a>, // compile error E0491
}

To fix this issue, you can specify a bound to the lifetime like below:

struct Struct<'a> {
    ref_i32: &'a i32,
}

// 'a: 'b means 'a outlives 'b
struct Nested<'a: 'b, 'b> {
    ref_struct: &'b Struct<'a>,
}

E0496

A lifetime name is shadowing another lifetime name. Erroneous code example:

struct Foo<'a> {
    a: &'a i32,
}

impl<'a> Foo<'a> {
    fn f<'a>(x: &'a i32) { // error: lifetime name `'a` shadows a lifetime
                           //        name that is already in scope
    }
}

Please change the name of one of the lifetimes to remove this error. Example:

struct Foo<'a> {
    a: &'a i32,
}

impl<'a> Foo<'a> {
    fn f<'b>(x: &'b i32) { // ok!
    }
}

fn main() {
}

E0497

A stability attribute was used outside of the standard library. Erroneous code example:

#[stable] // error: stability attributes may not be used outside of the
          //        standard library
fn foo() {}

It is not possible to use stability attributes outside of the standard library. Also, for now, it is not possible to write deprecation messages either.

E0499

A variable was borrowed as mutable more than once. Erroneous code example:

let mut i = 0;
let mut x = &mut i;
let mut a = &mut i;
// error: cannot borrow `i` as mutable more than once at a time

Please note that in rust, you can either have many immutable references, or one mutable reference. Take a look at https://doc.rust-lang.org/stable/book/references-and-borrowing.html for more information. Example:

let mut i = 0;
let mut x = &mut i; // ok!

// or:
let mut i = 0;
let a = &i; // ok!
let b = &i; // still ok!
let c = &i; // still ok!

E0500

A borrowed variable was used in another closure. Example of erroneous code:

fn you_know_nothing(jon_snow: &mut i32) {
    let nights_watch = || {
        *jon_snow = 2;
    };
    let starks = || {
        *jon_snow = 3; // error: closure requires unique access to `jon_snow`
                       //        but it is already borrowed
    };
}

In here, jon_snow is already borrowed by the nights_watch closure, so it cannot be borrowed by the starks closure at the same time. To fix this issue, you can put the closure in its own scope:

fn you_know_nothing(jon_snow: &mut i32) {
    {
        let nights_watch = || {
            *jon_snow = 2;
        };
    } // At this point, `jon_snow` is free.
    let starks = || {
        *jon_snow = 3;
    };
}

Or, if the type implements the Clone trait, you can clone it between closures:

fn you_know_nothing(jon_snow: &mut i32) {
    let mut jon_copy = jon_snow.clone();
    let nights_watch = || {
        jon_copy = 2;
    };
    let starks = || {
        *jon_snow = 3;
    };
}

E0501

This error indicates that a mutable variable is being used while it is still captured by a closure. Because the closure has borrowed the variable, it is not available for use until the closure goes out of scope.

Note that a capture will either move or borrow a variable, but in this situation, the closure is borrowing the variable. Take a look at http://rustbyexample.com/fn/closures/capture.html for more information about capturing.

Example of erroneous code:

fn inside_closure(x: &mut i32) {
    // Actions which require unique access
}

fn outside_closure(x: &mut i32) {
    // Actions which require unique access
}

fn foo(a: &mut i32) {
    let bar = || {
        inside_closure(a)
    };
    outside_closure(a); // error: cannot borrow `*a` as mutable because previous
                        //        closure requires unique access.
}

To fix this error, you can place the closure in its own scope:

fn inside_closure(x: &mut i32) {}
fn outside_closure(x: &mut i32) {}

fn foo(a: &mut i32) {
    {
        let bar = || {
            inside_closure(a)
        };
    } // borrow on `a` ends.
    outside_closure(a); // ok!
}

Or you can pass the variable as a parameter to the closure:

fn inside_closure(x: &mut i32) {}
fn outside_closure(x: &mut i32) {}

fn foo(a: &mut i32) {
    let bar = |s: &mut i32| {
        inside_closure(s)
    };
    outside_closure(a);
    bar(a);
}

It may be possible to define the closure later:

fn inside_closure(x: &mut i32) {}
fn outside_closure(x: &mut i32) {}

fn foo(a: &mut i32) {
    outside_closure(a);
    let bar = || {
        inside_closure(a)
    };
}

E0502

This error indicates that you are trying to borrow a variable as mutable when it has already been borrowed as immutable.

Example of erroneous code:

fn bar(x: &mut i32) {}
fn foo(a: &mut i32) {
    let ref y = a; // a is borrowed as immutable.
    bar(a); // error: cannot borrow `*a` as mutable because `a` is also borrowed
            //        as immutable
}

To fix this error, ensure that you don't have any other references to the variable before trying to access it mutably:

fn bar(x: &mut i32) {}
fn foo(a: &mut i32) {
    bar(a);
    let ref y = a; // ok!
}

For more information on the rust ownership system, take a look at https://doc.rust-lang.org/stable/book/references-and-borrowing.html.

E0503

A value was used after it was mutably borrowed.

Example of erroneous code:

fn main() {
    let mut value = 3;
    // Create a mutable borrow of `value`. This borrow
    // lives until the end of this function.
    let _borrow = &mut value;
    let _sum = value + 1; // error: cannot use `value` because
                          //        it was mutably borrowed
}

In this example, value is mutably borrowed by borrow and cannot be used to calculate sum. This is not possible because this would violate Rust's mutability rules.

You can fix this error by limiting the scope of the borrow:

fn main() {
    let mut value = 3;
    // By creating a new block, you can limit the scope
    // of the reference.
    {
        let _borrow = &mut value; // Use `_borrow` inside this block.
    }
    // The block has ended and with it the borrow.
    // You can now use `value` again.
    let _sum = value + 1;
}

Or by cloning value before borrowing it:

fn main() {
    let mut value = 3;
    // We clone `value`, creating a copy.
    let value_cloned = value.clone();
    // The mutable borrow is a reference to `value` and
    // not to `value_cloned`...
    let _borrow = &mut value;
    // ... which means we can still use `value_cloned`,
    let _sum = value_cloned + 1;
    // even though the borrow only ends here.
}

You can find more information about borrowing in the rust-book: http://doc.rust-lang.org/stable/book/references-and-borrowing.html

E0504

This error occurs when an attempt is made to move a borrowed variable into a closure.

Example of erroneous code:

struct FancyNum {
    num: u8,
}

fn main() {
    let fancy_num = FancyNum { num: 5 };
    let fancy_ref = &fancy_num;

    let x = move || {
        println!("child function: {}", fancy_num.num);
        // error: cannot move `fancy_num` into closure because it is borrowed
    };

    x();
    println!("main function: {}", fancy_ref.num);
}

Here, fancy_num is borrowed by fancy_ref and so cannot be moved into the closure x. There is no way to move a value into a closure while it is borrowed, as that would invalidate the borrow.

If the closure can't outlive the value being moved, try using a reference rather than moving:

struct FancyNum {
    num: u8,
}

fn main() {
    let fancy_num = FancyNum { num: 5 };
    let fancy_ref = &fancy_num;

    let x = move || {
        // fancy_ref is usable here because it doesn't move `fancy_num`
        println!("child function: {}", fancy_ref.num);
    };

    x();

    println!("main function: {}", fancy_num.num);
}

If the value has to be borrowed and then moved, try limiting the lifetime of the borrow using a scoped block:

struct FancyNum {
    num: u8,
}

fn main() {
    let fancy_num = FancyNum { num: 5 };

    {
        let fancy_ref = &fancy_num;
        println!("main function: {}", fancy_ref.num);
        // `fancy_ref` goes out of scope here
    }

    let x = move || {
        // `fancy_num` can be moved now (no more references exist)
        println!("child function: {}", fancy_num.num);
    };

    x();
}

If the lifetime of a reference isn't enough, such as in the case of threading, consider using an Arc to create a reference-counted value:

use std::sync::Arc;
use std::thread;

struct FancyNum {
    num: u8,
}

fn main() {
    let fancy_ref1 = Arc::new(FancyNum { num: 5 });
    let fancy_ref2 = fancy_ref1.clone();

    let x = thread::spawn(move || {
        // `fancy_ref1` can be moved and has a `'static` lifetime
        println!("child thread: {}", fancy_ref1.num);
    });

    x.join().expect("child thread should finish");
    println!("main thread: {}", fancy_ref2.num);
}

E0505

A value was moved out while it was still borrowed.

Erroneous code example:

struct Value {}

fn eat(val: Value) {}

fn main() {
    let x = Value{};
    {
        let _ref_to_val: &Value = &x;
        eat(x);
    }
}

Here, the function eat takes the ownership of x. However, x cannot be moved because it was borrowed to _ref_to_val. To fix that you can do few different things:

  • Try to avoid moving the variable.
  • Release borrow before move.
  • Implement the Copy trait on the type.

Examples:

struct Value {}

fn eat(val: &Value) {}

fn main() {
    let x = Value{};
    {
        let _ref_to_val: &Value = &x;
        eat(&x); // pass by reference, if it's possible
    }
}

Or:

struct Value {}

fn eat(val: Value) {}

fn main() {
    let x = Value{};
    {
        let _ref_to_val: &Value = &x;
    }
    eat(x); // release borrow and then move it.
}

Or:

#[derive(Clone, Copy)] // implement Copy trait
struct Value {}

fn eat(val: Value) {}

fn main() {
    let x = Value{};
    {
        let _ref_to_val: &Value = &x;
        eat(x); // it will be copied here.
    }
}

You can find more information about borrowing in the rust-book: http://doc.rust-lang.org/stable/book/references-and-borrowing.html

E0506

This error occurs when an attempt is made to assign to a borrowed value.

Example of erroneous code:

struct FancyNum {
    num: u8,
}

fn main() {
    let mut fancy_num = FancyNum { num: 5 };
    let fancy_ref = &fancy_num;
    fancy_num = FancyNum { num: 6 };
    // error: cannot assign to `fancy_num` because it is borrowed

    println!("Num: {}, Ref: {}", fancy_num.num, fancy_ref.num);
}

Because fancy_ref still holds a reference to fancy_num, fancy_num can't be assigned to a new value as it would invalidate the reference.

Alternatively, we can move out of fancy_num into a second fancy_num:

struct FancyNum {
    num: u8,
}

fn main() {
    let mut fancy_num = FancyNum { num: 5 };
    let moved_num = fancy_num;
    fancy_num = FancyNum { num: 6 };

    println!("Num: {}, Moved num: {}", fancy_num.num, moved_num.num);
}

If the value has to be borrowed, try limiting the lifetime of the borrow using a scoped block:

struct FancyNum {
    num: u8,
}

fn main() {
    let mut fancy_num = FancyNum { num: 5 };

    {
        let fancy_ref = &fancy_num;
        println!("Ref: {}", fancy_ref.num);
    }

    // Works because `fancy_ref` is no longer in scope
    fancy_num = FancyNum { num: 6 };
    println!("Num: {}", fancy_num.num);
}

Or by moving the reference into a function:

struct FancyNum {
    num: u8,
}

fn main() {
    let mut fancy_num = FancyNum { num: 5 };

    print_fancy_ref(&fancy_num);

    // Works because function borrow has ended
    fancy_num = FancyNum { num: 6 };
    println!("Num: {}", fancy_num.num);
}

fn print_fancy_ref(fancy_ref: &FancyNum){
    println!("Ref: {}", fancy_ref.num);
}

E0507

You tried to move out of a value which was borrowed. Erroneous code example:

use std::cell::RefCell;

struct TheDarkKnight;

impl TheDarkKnight {
    fn nothing_is_true(self) {}
}

fn main() {
    let x = RefCell::new(TheDarkKnight);

    x.borrow().nothing_is_true(); // error: cannot move out of borrowed content
}

Here, the nothing_is_true method takes the ownership of self. However, self cannot be moved because .borrow() only provides an &TheDarkKnight, which is a borrow of the content owned by the RefCell. To fix this error, you have three choices:

  • Try to avoid moving the variable.
  • Somehow reclaim the ownership.
  • Implement the Copy trait on the type.

Examples:

use std::cell::RefCell;

struct TheDarkKnight;

impl TheDarkKnight {
    fn nothing_is_true(&self) {} // First case, we don't take ownership
}

fn main() {
    let x = RefCell::new(TheDarkKnight);

    x.borrow().nothing_is_true(); // ok!
}

Or:

use std::cell::RefCell;

struct TheDarkKnight;

impl TheDarkKnight {
    fn nothing_is_true(self) {}
}

fn main() {
    let x = RefCell::new(TheDarkKnight);
    let x = x.into_inner(); // we get back ownership

    x.nothing_is_true(); // ok!
}

Or:

use std::cell::RefCell;

#[derive(Clone, Copy)] // we implement the Copy trait
struct TheDarkKnight;

impl TheDarkKnight {
    fn nothing_is_true(self) {}
}

fn main() {
    let x = RefCell::new(TheDarkKnight);

    x.borrow().nothing_is_true(); // ok!
}

Moving out of a member of a mutably borrowed struct is fine if you put something back. mem::replace can be used for that:

struct TheDarkKnight;

impl TheDarkKnight {
    fn nothing_is_true(self) {}
}

struct Batcave {
    knight: TheDarkKnight
}

fn main() {
    use std::mem;

    let mut cave = Batcave {
        knight: TheDarkKnight
    };
    let borrowed = &mut cave;

    borrowed.knight.nothing_is_true(); // E0507
    mem::replace(&mut borrowed.knight, TheDarkKnight).nothing_is_true(); // ok!
}

You can find more information about borrowing in the rust-book: http://doc.rust-lang.org/stable/book/references-and-borrowing.html

E0508

A value was moved out of a non-copy fixed-size array.

Example of erroneous code:

struct NonCopy;

fn main() {
    let array = [NonCopy; 1];
    let _value = array[0]; // error: cannot move out of type `[NonCopy; 1]`,
                           //        a non-copy fixed-size array
}

The first element was moved out of the array, but this is not possible because NonCopy does not implement the Copy trait.

Consider borrowing the element instead of moving it:

struct NonCopy;

fn main() {
    let array = [NonCopy; 1];
    let _value = &array[0]; // Borrowing is allowed, unlike moving.
}

Alternatively, if your type implements Clone and you need to own the value, consider borrowing and then cloning:

#[derive(Clone)]
struct NonCopy;

fn main() {
    let array = [NonCopy; 1];
    // Now you can clone the array element.
    let _value = array[0].clone();
}

E0509

This error occurs when an attempt is made to move out of a value whose type implements the Drop trait.

Example of erroneous code:

struct FancyNum {
    num: usize
}

struct DropStruct {
    fancy: FancyNum
}

impl Drop for DropStruct {
    fn drop(&mut self) {
        // Destruct DropStruct, possibly using FancyNum
    }
}

fn main() {
    let drop_struct = DropStruct{fancy: FancyNum{num: 5}};
    let fancy_field = drop_struct.fancy; // Error E0509
    println!("Fancy: {}", fancy_field.num);
    // implicit call to `drop_struct.drop()` as drop_struct goes out of scope
}

Here, we tried to move a field out of a struct of type DropStruct which implements the Drop trait. However, a struct cannot be dropped if one or more of its fields have been moved.

Structs implementing the Drop trait have an implicit destructor that gets called when they go out of scope. This destructor may use the fields of the struct, so moving out of the struct could make it impossible to run the destructor. Therefore, we must think of all values whose type implements the Drop trait as single units whose fields cannot be moved.

This error can be fixed by creating a reference to the fields of a struct, enum, or tuple using the ref keyword:

struct FancyNum {
    num: usize
}

struct DropStruct {
    fancy: FancyNum
}

impl Drop for DropStruct {
    fn drop(&mut self) {
        // Destruct DropStruct, possibly using FancyNum
    }
}

fn main() {
    let drop_struct = DropStruct{fancy: FancyNum{num: 5}};
    let ref fancy_field = drop_struct.fancy; // No more errors!
    println!("Fancy: {}", fancy_field.num);
    // implicit call to `drop_struct.drop()` as drop_struct goes out of scope
}

Note that this technique can also be used in the arms of a match expression:

struct FancyNum {
    num: usize
}

enum DropEnum {
    Fancy(FancyNum)
}

impl Drop for DropEnum {
    fn drop(&mut self) {
        // Destruct DropEnum, possibly using FancyNum
    }
}

fn main() {
    // Creates and enum of type `DropEnum`, which implements `Drop`
    let drop_enum = DropEnum::Fancy(FancyNum{num: 10});
    match drop_enum {
        // Creates a reference to the inside of `DropEnum::Fancy`
        DropEnum::Fancy(ref fancy_field) => // No error!
            println!("It was fancy-- {}!", fancy_field.num),
    }
    // implicit call to `drop_enum.drop()` as drop_enum goes out of scope
}

E0511

Invalid monomorphization of an intrinsic function was used. Erroneous code example:

#![feature(platform_intrinsics)]

extern "platform-intrinsic" {
    fn simd_add<T>(a: T, b: T) -> T;
}

fn main() {
    unsafe { simd_add(0, 1); }
    // error: invalid monomorphization of `simd_add` intrinsic
}

The generic type has to be a SIMD type. Example:

#![feature(repr_simd)]
#![feature(platform_intrinsics)]

#[repr(simd)]
#[derive(Copy, Clone)]
struct i32x1(i32);

extern "platform-intrinsic" {
    fn simd_add<T>(a: T, b: T) -> T;
}

unsafe { simd_add(i32x1(0), i32x1(1)); } // ok!

E0512

Transmute with two differently sized types was attempted. Erroneous code example:

fn takes_u8(_: u8) {}

fn main() {
    unsafe { takes_u8(::std::mem::transmute(0u16)); }
    // error: transmute called with differently sized types
}

Please use types with same size or use the expected type directly. Example:

fn takes_u8(_: u8) {}

fn main() {
    unsafe { takes_u8(::std::mem::transmute(0i8)); } // ok!
    // or:
    unsafe { takes_u8(0u8); } // ok!
}

E0516

The typeof keyword is currently reserved but unimplemented. Erroneous code example:

fn main() {
    let x: typeof(92) = 92;
}

Try using type inference instead. Example:

fn main() {
    let x = 92;
}

E0517

This error indicates that a #[repr(..)] attribute was placed on an unsupported item.

Examples of erroneous code:

#[repr(C)]
type Foo = u8;

#[repr(packed)]
enum Foo {Bar, Baz}

#[repr(u8)]
struct Foo {bar: bool, baz: bool}

#[repr(C)]
impl Foo {
    // ...
}
  • The #[repr(C)] attribute can only be placed on structs and enums.
  • The #[repr(packed)] and #[repr(simd)] attributes only work on structs.
  • The #[repr(u8)], #[repr(i16)], etc attributes only work on enums.

These attributes do not work on typedefs, since typedefs are just aliases.

Representations like #[repr(u8)], #[repr(i64)] are for selecting the discriminant size for C-like enums (when there is no associated data, e.g. enum Color {Red, Blue, Green}), effectively setting the size of the enum to the size of the provided type. Such an enum can be cast to a value of the same type as well. In short, #[repr(u8)] makes the enum behave like an integer with a constrained set of allowed values.

Only C-like enums can be cast to numerical primitives, so this attribute will not apply to structs.

#[repr(packed)] reduces padding to make the struct size smaller. The representation of enums isn't strictly defined in Rust, and this attribute won't work on enums.

#[repr(simd)] will give a struct consisting of a homogenous series of machine types (i.e. u8, i32, etc) a representation that permits vectorization via SIMD. This doesn't make much sense for enums since they don't consist of a single list of data.

E0518

This error indicates that an #[inline(..)] attribute was incorrectly placed on something other than a function or method.

Examples of erroneous code:

#[inline(always)]
struct Foo;

#[inline(never)]
impl Foo {
    // ...
}

#[inline] hints the compiler whether or not to attempt to inline a method or function. By default, the compiler does a pretty good job of figuring this out itself, but if you feel the need for annotations, #[inline(always)] and #[inline(never)] can override or force the compiler's decision.

If you wish to apply this attribute to all methods in an impl, manually annotate each method; it is not possible to annotate the entire impl with an #[inline] attribute.

E0520

A non-default implementation was already made on this type so it cannot be specialized further. Erroneous code example:

#![feature(specialization)]

trait SpaceLlama {
    fn fly(&self);
}

// applies to all T
impl<T> SpaceLlama for T {
    default fn fly(&self) {}
}

// non-default impl
// applies to all `Clone` T and overrides the previous impl
impl<T: Clone> SpaceLlama for T {
    fn fly(&self) {}
}

// since `i32` is clone, this conflicts with the previous implementation
impl SpaceLlama for i32 {
    default fn fly(&self) {}
    // error: item `fly` is provided by an `impl` that specializes
    //        another, but the item in the parent `impl` is not marked
    //        `default` and so it cannot be specialized.
}

Specialization only allows you to override default functions in implementations.

To fix this error, you need to mark all the parent implementations as default. Example:

#![feature(specialization)]

trait SpaceLlama {
    fn fly(&self);
}

// applies to all T
impl<T> SpaceLlama for T {
    default fn fly(&self) {} // This is a parent implementation.
}

// applies to all `Clone` T; overrides the previous impl
impl<T: Clone> SpaceLlama for T {
    default fn fly(&self) {} // This is a parent implementation but was
                             // previously not a default one, causing the error
}

// applies to i32, overrides the previous two impls
impl SpaceLlama for i32 {
    fn fly(&self) {} // And now that's ok!
}

E0522

The lang attribute is intended for marking special items that are built-in to Rust itself. This includes special traits (like Copy and Sized) that affect how the compiler behaves, as well as special functions that may be automatically invoked (such as the handler for out-of-bounds accesses when indexing a slice). Erroneous code example:

#![feature(lang_items)]

#[lang = "cookie"]
fn cookie() -> ! { // error: definition of an unknown language item: `cookie`
    loop {}
}

E0525

A closure was used but didn't implement the expected trait.

Erroneous code example:

struct X;

fn foo<T>(_: T) {}
fn bar<T: Fn(u32)>(_: T) {}

fn main() {
    let x = X;
    let closure = |_| foo(x); // error: expected a closure that implements
                              //        the `Fn` trait, but this closure only
                              //        implements `FnOnce`
    bar(closure);
}

In the example above, closure is an FnOnce closure whereas the bar function expected an Fn closure. In this case, it's simple to fix the issue, you just have to implement Copy and Clone traits on struct X and it'll be ok:

#[derive(Clone, Copy)] // We implement `Clone` and `Copy` traits.
struct X;

fn foo<T>(_: T) {}
fn bar<T: Fn(u32)>(_: T) {}

fn main() {
    let x = X;
    let closure = |_| foo(x);
    bar(closure); // ok!
}

To understand better how closures work in Rust, read: https://doc.rust-lang.org/book/closures.html

E0527

The number of elements in an array or slice pattern differed from the number of elements in the array being matched.

Example of erroneous code:

#![feature(slice_patterns)]

let r = &[1, 2, 3, 4];
match r {
    &[a, b] => { // error: pattern requires 2 elements but array
                 //        has 4
        println!("a={}, b={}", a, b);
    }
}

Ensure that the pattern is consistent with the size of the matched array. Additional elements can be matched with ..:

#![feature(slice_patterns)]

let r = &[1, 2, 3, 4];
match r {
    &[a, b, ..] => { // ok!
        println!("a={}, b={}", a, b);
    }
}

E0528

An array or slice pattern required more elements than were present in the matched array.

Example of erroneous code:

#![feature(slice_patterns)]

let r = &[1, 2];
match r {
    &[a, b, c, rest..] => { // error: pattern requires at least 3
                            //        elements but array has 2
        println!("a={}, b={}, c={} rest={:?}", a, b, c, rest);
    }
}

Ensure that the matched array has at least as many elements as the pattern requires. You can match an arbitrary number of remaining elements with ..:

#![feature(slice_patterns)]

let r = &[1, 2, 3, 4, 5];
match r {
    &[a, b, c, rest..] => { // ok!
        // prints `a=1, b=2, c=3 rest=[4, 5]`
        println!("a={}, b={}, c={} rest={:?}", a, b, c, rest);
    }
}

E0529

An array or slice pattern was matched against some other type.

Example of erroneous code:

#![feature(slice_patterns)]

let r: f32 = 1.0;
match r {
    [a, b] => { // error: expected an array or slice, found `f32`
        println!("a={}, b={}", a, b);
    }
}

Ensure that the pattern and the expression being matched on are of consistent types:

#![feature(slice_patterns)]

let r = [1.0, 2.0];
match r {
    [a, b] => { // ok!
        println!("a={}, b={}", a, b);
    }
}

E0530

A binding shadowed something it shouldn't.

Erroneous code example:

static TEST: i32 = 0;

let r: (i32, i32) = (0, 0);
match r {
    TEST => {} // error: match bindings cannot shadow statics
}

To fix this error, just change the binding's name in order to avoid shadowing one of the following:

  • struct name
  • struct/enum variant
  • static
  • const
  • associated const

Fixed example:

static TEST: i32 = 0;

let r: (i32, i32) = (0, 0);
match r {
    something => {} // ok!
}

E0532

Pattern arm did not match expected kind.

Erroneous code example:

enum State {
    Succeeded,
    Failed(String),
}

fn print_on_failure(state: &State) {
    match *state {
        // error: expected unit struct/variant or constant, found tuple
        //        variant `State::Failed`
        State::Failed => println!("Failed"),
        _ => ()
    }
}

To fix this error, ensure the match arm kind is the same as the expression matched.

Fixed example:

enum State {
    Succeeded,
    Failed(String),
}

fn print_on_failure(state: &State) {
    match *state {
        State::Failed(ref msg) => println!("Failed with {}", msg),
        _ => ()
    }
}

E0559

An unknown field was specified into an enum's structure variant.

Erroneous code example:

enum Field {
    Fool { x: u32 },
}

let s = Field::Fool { joke: 0 };
// error: struct variant `Field::Fool` has no field named `joke`

Verify you didn't misspell the field's name or that the field exists. Example:

enum Field {
    Fool { joke: u32 },
}

let s = Field::Fool { joke: 0 }; // ok!

E0560

An unknown field was specified into a structure.

Erroneous code example:

struct Simba {
    mother: u32,
}

let s = Simba { mother: 1, father: 0 };
// error: structure `Simba` has no field named `father`

Verify you didn't misspell the field's name or that the field exists. Example:

struct Simba {
    mother: u32,
    father: u32,
}

let s = Simba { mother: 1, father: 0 }; // ok!

E0569

If an impl has a generic parameter with the #[may_dangle] attribute, then that impl must be declared as an `unsafe impl. For example:

#![feature(generic_param_attrs)]
#![feature(dropck_eyepatch)]

struct Foo<X>(X);
impl<#[may_dangle] X> Drop for Foo<X> {
    fn drop(&mut self) { }
}

In this example, we are asserting that the destructor for Foo will not access any data of type X, and require this assertion to be true for overall safety in our program. The compiler does not currently attempt to verify this assertion; therefore we must tag this impl as unsafe.

E0570

The requested ABI is unsupported by the current target.

The rust compiler maintains for each target a blacklist of ABIs unsupported on that target. If an ABI is present in such a list this usually means that the target / ABI combination is currently unsupported by llvm.

If necessary, you can circumvent this check using custom target specifications.

E0572

A return statement was found outside of a function body.

Erroneous code example:

const FOO: u32 = return 0; // error: return statement outside of function body

fn main() {}

To fix this issue, just remove the return keyword or move the expression into a function. Example:

const FOO: u32 = 0;

fn some_fn() -> u32 {
    return FOO;
}

fn main() {
    some_fn();
}

E0580

The main function was incorrectly declared.

Erroneous code example:

fn main() -> i32 { // error: main function has wrong type
    0
}

The main function prototype should never take arguments or return type. Example:

fn main() {
    // your code
}

If you want to get command-line arguments, use std::env::args. To exit with a specified exit code, use std::process::exit.

E0581

In a fn type, a lifetime appears only in the return type, and not in the arguments types.

Erroneous code example:

fn main() {
    // Here, `'a` appears only in the return type:
    let x: for<'a> fn() -> &'a i32;
}

To fix this issue, either use the lifetime in the arguments, or use 'static. Example:

fn main() {
    // Here, `'a` appears only in the return type:
    let x: for<'a> fn(&'a i32) -> &'a i32;
    let y: fn() -> &'static i32;
}

Note: The examples above used to be (erroneously) accepted by the compiler, but this was since corrected. See issue #33685 for more details.

E0582

A lifetime appears only in an associated-type binding, and not in the input types to the trait.

Erroneous code example:

fn bar<F>(t: F)
    // No type can satisfy this requirement, since `'a` does not
    // appear in any of the input types (here, `i32`):
    where F: for<'a> Fn(i32) -> Option<&'a i32>
{
}

fn main() { }

To fix this issue, either use the lifetime in the inputs, or use 'static. Example:

fn bar<F, G>(t: F, u: G)
    where F: for<'a> Fn(&'a i32) -> Option<&'a i32>,
          G: Fn(i32) -> Option<&'static i32>,
{
}

fn main() { }

Note: The examples above used to be (erroneously) accepted by the compiler, but this was since corrected. See issue #33685 for more details.

E0591

Per RFC 401, if you have a function declaration foo:

// For the purposes of this explanation, all of these
// different kinds of `fn` declarations are equivalent:
fn foo(x: i32) { ... }
extern "C" fn foo(x: i32);
impl i32 { fn foo(x: self) { ... } }

the type of foo is not fn(i32), as one might expect. Rather, it is a unique, zero-sized marker type written here as typeof(foo). However, typeof(foo) can be coerced to a function pointer fn(i32), so you rarely notice this:

let x: fn(i32) = foo; // OK, coerces

The reason that this matter is that the type fn(i32) is not specific to any particular function: it's a function pointer. So calling x() results in a virtual call, whereas foo() is statically dispatched, because the type of foo tells us precisely what function is being called.

As noted above, coercions mean that most code doesn't have to be concerned with this distinction. However, you can tell the difference when using transmute to convert a fn item into a fn pointer.

This is sometimes done as part of an FFI:

extern "C" fn foo(userdata: Box<i32>) {
   ...
}

let f: extern "C" fn(*mut i32) = transmute(foo);
callback(f);

Here, transmute is being used to convert the types of the fn arguments. This pattern is incorrect because, because the type of foo is a function item (typeof(foo)), which is zero-sized, and the target type (fn()) is a function pointer, which is not zero-sized. This pattern should be rewritten. There are a few possible ways to do this:

  • change the original fn declaration to match the expected signature, and do the cast in the fn body (the prefered option)
  • cast the fn item fo a fn pointer before calling transmute, as shown here:
    • let f: extern "C" fn(*mut i32) = transmute(foo as extern "C" fn(_))
    • let f: extern "C" fn(*mut i32) = transmute(foo as usize) /* works too */

The same applies to transmutes to *mut fn(), which were observedin practice. Note though that use of this type is generally incorrect. The intention is typically to describe a function pointer, but just fn() alone suffices for that. *mut fn() is a pointer to a fn pointer. (Since these values are typically just passed to C code, however, this rarely makes a difference in practice.)

© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/error-index.html

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部