```
enum Fruit {
- Apple(String, String)
- Pear(u32)
+ 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 {
- Apple(a, b) => ...
+ Fruit::Apple(a, b) => {},
+ _ => {}
}
```
Matching with the wrong number of fields has no sensible interpretation:
-```
+```compile_fail
+enum Fruit {
+ Apple(String, String),
+ Pear(u32),
+}
+
+let x = Fruit::Apple(String::new(), String::new());
+
// Incorrect.
match x {
- Apple(a) => ...,
- Apple(a, b, c) => ...
+ Apple(a) => {},
+ Apple(a, b, c) => {},
}
```
This error indicates that a pattern attempted to extract the fields of an enum
variant with no fields. Here's a tiny example of this error:
-```
+```compile_fail
// This enum has two variants.
enum Number {
// This variant has no fields.
// Assuming x is a Number we can pattern match on its contents.
match x {
- Zero(inside) => ...,
- One(inside) => ...
+ Zero(inside) => {},
+ One(inside) => {},
}
```
Each field of a struct can only be bound once in a pattern. Erroneous code
example:
-```
+```compile_fail
struct Foo {
a: u8,
b: u8,
}
let thing = Thing { x: 1, y: 2 };
+
match thing {
- Thing { x: xfield, y: yfield } => ...
+ 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:
+Change this:
+
+```compile_fail
+struct Thing {
+ x: u32,
+ y: u32
+}
+
+let thing = Thing { x: 0, y: 0 };
+
match thing {
- Thing { x, z } => ...
+ Thing { x, z } => {}
+}
+```
+
+To this:
+
+```
+struct Thing {
+ x: u32,
+ y: u32
}
-// To this:
+let thing = Thing { x: 0, y: 0 };
+
match thing {
- Thing { x, y: z } => ...
+ Thing { x, y: z } => {}
}
```
"##,
For example:
-```
+```compile_fail
struct Dog {
name: String,
- age: u32
+ age: u32,
}
let d = Dog { name: "Rusty".to_string(), age: 8 };
// This is incorrect.
match d {
- Dog { age: x } => ...
+ Dog { age: x } => {}
}
+```
+
+This is correct (explicit):
+
+```
+struct Dog {
+ name: String,
+ age: u32,
+}
+
+let d = Dog { name: "Rusty".to_string(), age: 8 };
-// This is correct (explicit).
match d {
- Dog { name: n, age: x } => ...
+ Dog { name: ref n, age: x } => {}
}
// This is also correct (ignore unused fields).
match d {
- Dog { age: x, .. } => ...
+ Dog { age: x, .. } => {}
}
```
"##,
want to capture values of an orderable type between two end-points, you can use
a guard.
-```
+```compile_fail
// The ordering relation for strings can't be evaluated at compile time,
// so this doesn't work:
match string {
- "hello" ... "world" => ...
- _ => ...
+ "hello" ... "world" => {}
+ _ => {}
}
// This is a more general version, using a guard:
match string {
- s if s >= "hello" && s <= "world" => ...
- _ => ...
+ s if s >= "hello" && s <= "world" => {}
+ _ => {}
}
```
"##,
Therefore, all accesses to trait types must be through pointers. If you
encounter this error you should try to avoid dereferencing the pointer.
-```
+```ignore
let trait_obj: &SomeTrait = ...;
// This tries to implicitly dereference to create an unsized local variable.
E0034: r##"
The compiler doesn't know what method to call because more than one method
-has the same prototype. Example:
+has the same prototype. Erroneous code example:
-```
+```compile_fail
struct Test;
trait Trait1 {
"##,
E0035: r##"
-You tried to give a type parameter where it wasn't needed. Bad example:
+You tried to give a type parameter where it wasn't needed. Erroneous code
+example:
-```
+```compile_fail
struct Test;
impl Test {
E0036: r##"
This error occurrs when you pass too many or not enough type parameters to
-a method. Example:
+a method. Erroneous code example:
-```
+```compile_fail
struct Test;
impl Test {
Please note on the last example that we could have called `method` like this:
-```
+```ignore
x.method(v);
```
"##,
Here's an example of this error:
-```
+```compile_fail
struct Foo {
x: i32,
}
E0044: r##"
You can't use type parameters on foreign items. Example of erroneous code:
-```
+```compile_fail
extern { fn some_func<T>(x: T); }
```
FFI. As such, variadic parameters can only be used with functions which are
using the C ABI. Examples of erroneous code:
-```
+```compile_fail
extern "rust-call" { fn foo(x: u8, ...); }
+
// or
+
fn foo(x: u8, ...) {}
```
To fix such code, put them in an extern "C" block:
+```ignore
+extern "C" fn foo(x: u8, ...);
+```
+
+Or:
+
```
-extern "C" fn foo (x: u8, ...);
-// or:
extern "C" {
fn foo (x: u8, ...);
}
E0046: r##"
Items are missing in a trait implementation. Erroneous code example:
-```
+```compile_fail
trait Foo {
fn foo();
}
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:
-```
+```compile_fail
trait Foo {
fn foo<T: Default>(x: T) -> Self;
}
(`&self` and `u8`), but the implementation of `foo` for the type `Bar` omits
the `u8` parameter:
-```
+```compile_fail
trait Foo {
fn foo(&self, x: u8) -> bool;
}
Here are a couple examples of this error:
-```
+```compile_fail
trait Foo {
fn foo(x: u16);
fn bar(&self);
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:
+```compile_fail
+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;
-
-// Not allowed, won't compile
-let x_is_nonzero = x as bool;
```
"##,
For a somewhat artificial example:
-```
+```compile_fail
#![recursion_limit="2"]
struct Foo;
An example using a closure:
-```
+```compile_fail
let f = |x| x * 3;
let a = f(); // invalid, too few parameters
let b = f(4); // this works!
The most likely source of this error is using angle-bracket notation without
wrapping the function argument type into a tuple, for example:
-```
+```compile_fail
fn foo<F: Fn<i32>>(f: F) -> F::Output { f(3) }
```
It can be fixed by adjusting the trait bound like this:
-```
+```ignore
fn foo<F: Fn<(i32,)>>(f: F) -> F::Output { f(3) }
```
takes a minimum number of arguments. For example, consider C's variadic `printf`
function:
-```
+```ignore
extern crate libc;
use libc::{ 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:
-```
+```ignore
unsafe {
use std::ffi::CString;
The number of arguments passed to a function must match the number of arguments
specified in the function signature.
-For example, a function like
+For example, a function like:
```
fn f(a: u16, b: &str) {}
```
-must always be called with exactly two arguments, e.g. `f(2, "test")`.
+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).
enum variant, one of the fields was specified more than once. Erroneous code
example:
-```
+```compile_fail
struct Foo {
x: i32
}
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:
-```
+```compile_fail
struct Foo {
x: i32,
y: i32
item paths (ie, namespaced variables), dereferences, indexing expressions,
and field references.
-Let's start with some bad examples:
+Let's start with some erroneous code examples:
-```
+```compile_fail
use std::collections::LinkedList;
// Bad: assignment to non-lvalue expression
}
```
-And now some good examples:
+And now some working examples:
```
let mut i : i32 = 0;
The compiler found a function whose body contains a `return;` statement but
whose return type is not `()`. An example of this is:
-```
+```compile_fail
// error
fn foo() -> u8 {
return;
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 bad examples:
+Now, we can go further. Here are some erroneous code examples:
-```
+```compile_fail
struct SomeStruct {
x: i32,
y: i32
}
+
const SOME_CONST : i32 = 12;
fn some_other_func() {}
}
```
-And now let's give good examples:
+And now let's give working examples:
```
struct SomeStruct {
Example of erroneous code:
-```
+```compile_fail
enum Foo { FirstValue(i32) };
let u = Foo::FirstValue { value: 0i32 }; // error: Foo::FirstValue
Here's an example of a struct that has this problem:
-```
+```compile_fail
struct Foo { x: Box<Foo> } // error
```
reason about how to use SIMD with them. This error will occur if the types
are generic.
-```
+This will cause an error:
+
+```compile_fail
+#![feature(simd)]
+
#[simd]
-struct Bad<T>(T, T, T); // This will cause an error
+struct Bad<T>(T, T, T);
+```
+
+This will not:
+
+```
+#![feature(simd)]
#[simd]
-struct Good(u32, u32, u32); // This will not
+struct Good(u32, u32, u32);
```
"##,
it doesn't make sense to try to use SIMD operations when there are no values to
operate on.
-```
+This will cause an error:
+
+```compile_fail
+#![feature(simd)]
+
#[simd]
-struct Bad; // This will cause an error
+struct Bad;
+```
+
+This will not:
+
+```
+#![feature(simd)]
#[simd]
-struct Good(u32); // This will not
+struct Good(u32);
```
"##,
struct, the types in the struct must all be of the same type, or the compiler
will trigger this error.
-```
-#[simd]
-struct Bad(u16, u32, u32); // This will cause an error
+This will cause an error:
+
+```compile_fail
+#![feature(simd)]
#[simd]
-struct Good(u32, u32, u32); // This will not
+struct Bad(u16, u32, u32);
```
+This will not:
+
+```
+#![feature(simd)]
+
+#[simd]
+struct Good(u32, u32, u32);
+```
"##,
E0077: r##"
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:
+
+```compile_fail
+#![feature(simd)]
+
#[simd]
-struct Bad(String); // This will cause an error
+struct Bad(String);
+```
+
+This will not:
+
+```
+#![feature(simd)]
#[simd]
-struct Good(u32, u32, u32); // This will not
+struct Good(u32, u32, u32);
```
"##,
representation. This error indicates that the value provided is not an integer
literal and is therefore invalid.
-For example, in the following code,
+For example, in the following code:
-```
+```compile_fail
enum Foo {
Q = "32"
}
```
-we try to set the representation to a string.
+We try to set the representation to a string.
There's no general fix for this; if you can work with an integer then just set
it to one:
}
```
-however if you actually wanted a mapping between variants and non-integer
+However if you actually wanted a mapping between variants and non-integer
objects, it may be preferable to use a method with a match instead:
```
integer expression provided as an enum discriminant. Attempting to divide by 0
or causing integer overflow are two ways to induce this error. For example:
-```
+```compile_fail
enum Enum {
X = (1 << 500),
Y = (1 / 0)
This error indicates that the same value was used for two or more variants,
making them impossible to tell apart.
-```
-// Good.
+```compile_fail
+// Bad.
enum Enum {
- P,
+ P = 3,
X = 3,
Y = 5
}
+```
-// Bad.
+```
+// Good.
enum Enum {
- P = 3,
+ P,
X = 3,
Y = 5
}
top to bottom starting from 0, so clashes can occur with seemingly unrelated
variants.
-```
+```compile_fail
enum Bad {
X,
Y = 0
an integer literal given as a discriminant is not a member of the discriminant
type. For example:
-```
+```compile_fail
#[repr(u8)]
enum Thing {
A = 1024,
E0087: r##"
Too many type parameters were supplied for a function. For example:
-```
+```compile_fail
fn foo<T>() {}
fn main() {
E0088: r##"
You gave too many lifetime parameters. Erroneous code example:
-```
+```compile_fail
fn f() {}
fn main() {
E0089: r##"
Not enough type parameters were supplied for a function. For example:
-```
+```compile_fail
fn foo<T, U>() {}
fn main() {
Note that if a function takes multiple type parameters but you want the compiler
to infer some of them, you can use type placeholders:
-```
+```compile_fail
fn foo<T, U>(x: T) {}
fn main() {
You gave an unnecessary type parameter in a type alias. Erroneous code
example:
-```
+```compile_fail
type Foo<T> = u32; // error: type parameter `T` is unused
// or:
type Foo<A,B> = Box<A>; // error: type parameter `B` is unused
```
type Foo = u32; // ok!
-type Foo<A> = Box<A>; // ok!
+type Foo2<A> = Box<A>; // ok!
```
"##,
You tried to declare an undefined atomic operation function.
Erroneous code example:
-```
+```compile_fail
#![feature(intrinsics)]
extern "rust-intrinsic" {
E0093: r##"
You declared an unknown intrinsic function. Erroneous code example:
-```
+```compile_fail
#![feature(intrinsics)]
extern "rust-intrinsic" {
You gave an invalid number of type parameters to an intrinsic function.
Erroneous code example:
-```
+```compile_fail
#![feature(intrinsics)]
extern "rust-intrinsic" {
You hit this error because the compiler lacks the information to
determine a type for this expression. Erroneous code example:
-```
+```compile_fail
fn main() {
let x = |_| {}; // error: cannot determine a type for this expression
}
You hit this error because the compiler lacks information to
determine a type for this variable. Erroneous code example:
-```
+```compile_fail
fn demo(devil: fn () -> !) {
let x: &_ = devil();
// error: cannot determine a type for this local variable
Examples:
```
+#![allow(unused_variables)]
+
fn some_func(x: &u32) {
// some code
}
Here are some simple examples of where you'll run into this error:
-```
+```compile_fail
struct Foo { x: &bool } // error
struct Foo<'a> { x: &'a bool } // correct
Here are some examples of elision errors:
-```
+```compile_fail
// error, no input lifetimes
-fn foo() -> &str { ... }
+fn foo() -> &str { }
// error, `x` and `y` have distinct lifetimes inferred
-fn bar(x: &str, y: &str) -> &str { ... }
+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 { ... }
+fn baz<'a>(x: &'a str, y: &str) -> &str { }
```
[book-le]: https://doc.rust-lang.org/nightly/book/lifetimes.html#lifetime-elision
Some basic examples include:
-```
+```compile_fail
struct Foo<'a>(&'a str);
enum Bar { A, B, C }
Here's an example that is currently an error, but may work in a future version
of Rust:
-```
+```compile_fail
struct Foo<'a>(&'a str);
trait Quux { }
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
+```compile_fail
+impl Vec<u8> { } // error
```
To fix this problem, you can do either of these things:
Note that using the `type` keyword does not work here because `type` only
introduces a type alias:
-```
+```compile_fail
type Bytes = Vec<u8>;
-impl Bytes { ... } // error, same as above
+impl Bytes { } // error, same as above
```
"##,
Here's one example of this error:
-```
+```compile_fail
impl Drop for u32 {}
```
To avoid this kind of error, ensure that at least one local type is referenced
by the `impl`:
-```
+```ignore
pub struct Foo; // you define your type in your crate
impl Drop for Foo { // and you can implement the trait on it!
You're trying to write an inherent implementation for something which isn't a
struct nor an enum. Erroneous code example:
-```
+```compile_fail
impl (u8, u8) { // error: no base type found for inherent implementation
fn get_state(&self) -> String {
// ...
There are conflicting trait implementations for the same type.
Example of erroneous code:
-```
+```compile_fail
trait MyTrait {
fn get(&self) -> usize;
}
this is an error. So, when you write:
```
+trait MyTrait {
+ fn get(&self) -> usize;
+}
+
impl<T> MyTrait for T {
fn get(&self) -> usize { 0 }
}
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:
-```
+```compile_fail
trait MyTrait {}
impl Drop for MyTrait {
Examples of this error include:
-```
+```compile_fail
fn foo() -> _ { 5 } // error, explicitly write out the return type instead
static BAR: _ = "test"; // error, explicitly write out the type instead
You declared two fields of a struct with the same name. Erroneous code
example:
-```
+```compile_fail
struct Foo {
field1: i32,
- field1: i32 // error: field is already declared
+ field1: i32, // error: field is already declared
}
```
```
struct Foo {
field1: i32,
- field2: i32 // ok!
+ field2: i32, // ok!
}
```
"##,
Type parameter defaults can only use parameters that occur before them.
Erroneous code example:
-```
-pub struct Foo<T=U, U=()> {
+```compile_fail
+struct Foo<T=U, U=()> {
field1: T,
filed2: U,
}
by doing:
```
-pub struct Foo<U=(), T=U> {
+struct Foo<U=(), T=U> {
field1: T,
filed2: U,
}
You declared a pattern as an argument in a foreign function declaration.
Erroneous code example:
-```
+```compile_fail
extern {
fn foo((a, b): (u32, u32)); // error: patterns aren't allowed in foreign
// function declarations
extern {
fn foo(s: SomeStruct); // ok!
}
-// or
+```
+
+Or:
+
+```
extern {
fn foo(a: (u32, u32)); // ok!
}
parameters. When `main` is present, it must take no arguments and return `()`.
Erroneous code example:
-```
+```compile_fail
fn main<T>() { // error: main function is not allowed to have type parameters
}
```
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:
-```
+```ignore
fn(isize, *const *const u8) -> isize;
```
"##,
This error means that an attempt was made to match an enum variant as a
struct type when the variant isn't a struct type:
-```
+```compile_fail
enum Foo { B(u32) }
fn bar(foo: Foo) -> u32 {
match foo {
- Foo::B{i} => i // error 0163
+ B{i} => i, // error E0163
}
}
```
Try using `()` instead:
```
+enum Foo { B(u32) }
+
fn bar(foo: Foo) -> u32 {
match foo {
- Foo::B(i) => i
+ Foo::B(i) => i,
}
}
```
"##,
E0164: r##"
-
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 } }
+```compile_fail
+enum Foo { B { i: u32 } }
fn bar(foo: Foo) -> u32 {
match foo {
- Foo::B(i) => i // error 0164
+ 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
+ Foo::B{i} => i,
}
}
```
"##,
-
E0166: r##"
This error means that the compiler found a return expression in a function
marked as diverging. A function diverges if it has `!` in the place of the
return type in its signature. For example:
-```
+```compile_fail
fn foo() -> ! { return; } // error
```
This error means that an attempt was made to specify the type of a variable with
a combination of a concrete type and a trait. Consider the following example:
-```
+```compile_fail
fn foo(bar: i32+std::fmt::Display) {}
```
For example:
-```
+```compile_fail
trait Foo {}
struct Bar<'a> {
Here's an example of this error:
-```
+```compile_fail
trait Foo {
fn foo();
}
Here's an example of this error:
-```
+```compile_fail
trait Foo {
fn foo(&self);
}
Trait objects need to have all associated types specified. Erroneous code
example:
-```
+```compile_fail
trait Trait {
type Bar;
}
`where` clauses must use generic type parameters: it does not make sense to use
them otherwise. An example causing this error:
-```
+```compile_fail
trait Foo {
fn bar(&self);
}
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) { }
}
A type parameter was declared which shadows an existing one. An example of this
error:
-```
+```compile_fail
trait Foo<T> {
fn do_something(&self) -> T;
fn do_something_else<T: Clone>(&self, bar: T);
Your method's lifetime parameters do not match the trait declaration.
Erroneous code example:
-```
+```compile_fail
trait Trait {
fn bar<'a,'b:'a>(x: &'a str, y: &'b str);
}
implementing an unsafe trait. Removing the `unsafe` keyword from the inherent
implementation will resolve this error.
-```
+```compile_fail
struct Foo;
// this will cause this error
so negative implementations are always safe and never need to be marked as
unsafe.
-```
+```compile_fail
+#![feature(optin_builtin_traits)]
+
struct Foo;
// unsafe is unnecessary
unsafe impl !Clone for Foo { }
-// this will compile
-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: r##"
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.
+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.
-```
+```compile_fail
struct Foo;
trait Bar { }
implementation for an unsafe trait isn't marked as unsafe. This may be resolved
by marking the unsafe implementation as unsafe.
-```
+```compile_fail
struct Foo;
unsafe trait Bar { }
For example:
-```
+```compile_fail
struct Foo(u8);
impl Foo {
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
-```
+```compile_fail
struct Foo {
foo : Vec<u32>,
}
Here's another example that will fail:
-```
+```compile_fail
#[derive(Copy)]
struct Foo<'a> {
ty: &'a mut bool,
variants does not implement `Copy`. To fix this, you must implement `Copy` for
the mentioned variant. Note that this may not be possible, as in the example of
-```
+```compile_fail
enum Foo {
Bar(Vec<u32>),
Baz,
Here's another example that will fail:
-```
+```compile_fail
#[derive(Copy)]
enum Foo<'a> {
Bar(&'a mut bool),
examples will fail, because neither `i32` (primitive type) nor `&'static Bar`
(reference to `Bar`) is a struct or enum:
-```
+```compile_fail
type Foo = i32;
impl Copy for Foo { } // error
You declared an unused type parameter when implementing a trait on an object.
Erroneous code example:
-```
+```compile_fail
trait MyTrait {
fn get(&self) -> usize;
}
If `ForeignTrait` is a trait defined in some external crate `foo`, then the
following trait `impl` is an error:
-```
+```compile_fail
extern crate foo;
use foo::ForeignTrait;
-impl<T> ForeignTrait for T { ... } // error
+impl<T> ForeignTrait for T { } // error
```
To work around this, it can be covered with a local type, `MyType`:
-```
+```ignore
struct MyType<T>(T);
-impl<T> ForeignTrait for MyType<T> { ... } // Ok
+impl<T> ForeignTrait for MyType<T> { } // Ok
```
Please note that a type alias is not sufficient.
named `ForeignTrait2` that takes two type parameters. Then this `impl` results
in the same rule violation:
-```
+```compile_fail
struct MyType2;
-impl<T> ForeignTrait2<T, MyType<T>> for MyType2 { ... } // error
+impl<T> ForeignTrait2<T, MyType<T>> for MyType2 { } // error
```
The reason for this is that there are two appearances of type parameter `T` in
Consider one more example:
-```
-impl<T> ForeignTrait2<MyType<T>, T> for MyType2 { ... } // Ok
+```ignore
+impl<T> ForeignTrait2<MyType<T>, T> for MyType2 { } // Ok
```
This only differs from the previous `impl` in that the parameters `T` and
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`:
-```
+```ignore
impl<P1, ..., Pm> ForeignTrait<T1, ..., Tn> for T0 { ... }
```
You used a function or type which doesn't fit the requirements for where it was
used. Erroneous code examples:
-```
+```compile_fail
#![feature(intrinsics)]
extern "rust-intrinsic" {
The second case example is a bit particular : the main function must always
have this definition:
-```
+```compile_fail
fn main();
```
```
let x = 1u8;
+
match x {
0u8...3u8 => (), // ok!
_ => ()
A generic type was described using parentheses rather than angle brackets. For
example:
-```
+```compile_fail
fn main() {
let v: Vec(&str) = vec!["foo"];
}
You used an associated type which isn't defined in the trait.
Erroneous code example:
-```
+```compile_fail
trait Trait {
type Bar;
}
An attempt was made to retrieve an associated type, but the type was ambiguous.
For example:
-```
+```compile_fail
trait T1 {}
trait T2 {}
following syntax:
```
-fn do_something() {
- let _: <Self as Bar>::A;
+trait T1 {}
+trait T2 {}
+
+trait Foo {
+ type A: T1;
+}
+
+trait Bar : Foo {
+ type A: T2;
+ fn do_something() {
+ let _: <Self as Bar>::A;
+ }
}
```
"##,
An attempt was made to retrieve an associated type, but the type was ambiguous.
For example:
-```
+```compile_fail
trait MyTrait {type X; }
fn main() {
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:
-```
+```compile_fail
fn main() {
let _: Box<std::io::Read+std::io::Write>;
}
E0232: r##"
The attribute must have a value. Erroneous code example:
-```
+```compile_fail
+#![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 {}
```
For example, the `Foo` struct below is defined to be generic in `T`, but the
type parameter is missing in the definition of `Bar`:
-```
+```compile_fail
struct Foo<T> { x: T }
struct Bar { x: Foo }
For example, the `Foo` struct below has no type parameters, but is supplied
with two in the definition of `Bar`:
-```
+```compile_fail
struct Foo { x: bool }
struct Bar<S, T> { x: Foo<S, T> }
This error indicates an attempt to use a value where a type is expected. For
example:
-```
+```compile_fail
enum Foo {
Bar(u32)
}
Some examples of code that produces this error are:
-```
+```compile_fail
const A: [u32; "hello"] = []; // error
const B: [u32; true] = []; // error
const C: [u32; 0.0] = []; // error
Some examples of this error are:
-```
+```compile_fail
// divide by zero in the length expression
const A: [u32; 1/0] = [];
A cross-crate opt-out trait was implemented on something which wasn't a struct
or enum type. Erroneous code example:
-```
+```compile_fail
#![feature(optin_builtin_traits)]
struct Foo;
An associated const was implemented when another trait item was expected.
Erroneous code example:
-```
+```compile_fail
+#![feature(associated_consts)]
+
trait Foo {
type N;
}
impl Foo for Bar {
type N = u32; // ok!
}
+```
+
+Or:
+
+```
+#![feature(associated_consts)]
+
+struct Bar;
-// or:
trait Foo {
const N : u32;
}
A method was implemented when another trait item was expected. Erroneous
code example:
-```
+```compile_fail
struct Bar;
trait Foo {
verify that you are indeed implementing the correct trait items. Example:
```
+#![feature(associated_consts)]
+
struct Bar;
trait Foo {
An associated type was implemented when another trait item was expected.
Erroneous code example:
-```
+```compile_fail
struct Bar;
trait Foo {
impl Foo for Bar {
type N = u32; // ok!
}
+```
+
+Or:
+
+```
+#![feature(associated_consts)]
+
+struct Bar;
-//or:
trait Foo {
const N : u32;
}
Here's an example of this error:
-```
+```compile_fail
trait Foo {
const BAR: bool;
}
You cannot use associated items other than constant items as patterns. This
includes method items. Example of erroneous code:
-```
+```compile_fail
enum B {}
impl B {
type parameter or `Self`. This is not supported yet. An example causing this
error is shown below:
-```
+```compile_fail
+#![feature(associated_consts)]
+
trait Foo {
const BAR: f64;
}
}
```
-Currently, the value of `BAR` for a particular type can only be accessed through
-a concrete type, as shown below:
+Currently, the value of `BAR` for a particular type can only be accessed
+through a concrete type, as shown below:
+
+```ignore
+#![feature(associated_consts)]
+
+trait Foo {
+ const BAR: f64;
+}
+
+struct MyStruct;
-```
fn get_bar_good() -> f64 {
<MyStruct as Foo>::BAR
}
An attempt was made to implement `Drop` on a concrete specialization of a
generic type. An example is shown below:
-```
+```compile_fail
struct Foo<T> {
t: T
}
An attempt was made to implement `Drop` on a specialization of a generic type.
An example is shown below:
-```
+```compile_fail
trait Foo{}
struct MyStruct<T> {
This error indicates that a binary assignment operator like `+=` or `^=` was
applied to a type that doesn't support it. For example:
-```
+```compile_fail
let mut x = 12f32; // error: binary operation `<<` cannot be applied to
- // type `f32`
+ // type `f32`
x <<= 2;
```
To fix this error, please check that this type implements this binary
operation. Example:
-```
+```compile_fail
let x = 12u32; // the `u32` type does implement the `ShlAssign` trait
x <<= 2; // ok!
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:
-```
+```compile_fail
use std::ops::Add;
struct Foo(u32);
A binary operation was attempted on a type which doesn't support it.
Erroneous code example:
-```
+```compile_fail
let x = 12f32; // error: binary operation `<<` cannot be applied to
// type `f32`
The maximum value of an enum was reached, so it cannot be automatically
set in the next enum value. Erroneous code example:
-```
+```compile_fail
enum Foo {
X = 0x7fffffffffffffff,
- Y // error: enum discriminant overflowed on value after
- // 9223372036854775807: i64; set explicitly via
- // Y = -9223372036854775808 if that is desired outcome
+ Y, // error: enum discriminant overflowed on value after
+ // 9223372036854775807: i64; set explicitly via
+ // Y = -9223372036854775808 if that is desired outcome
}
```
X = 0x7fffffffffffffff,
Y = 0, // ok!
}
+```
-// or:
+Or:
+
+```
enum Foo {
Y = 0, // ok!
X = 0x7fffffffffffffff,
Example:
-```
+```compile_fail
trait Foo { fn foo(&self) { } }
trait Bar: Foo { }
trait Baz: Bar { }
E0390: r##"
You tried to implement methods for a primitive type. Erroneous code example:
-```
+```compile_fail
struct Foo {
x: i32
}
The following example contains a circular dependency between two traits:
-```
+```compile_fail
trait FirstTrait : SecondTrait {
}
E0392: r##"
This error indicates that a type or lifetime parameter has been declared
-but not actually used. Here is an example that demonstrates the error:
+but not actually used. Here is an example that demonstrates the error:
-```
+```compile_fail
enum Foo<T> {
Bar
}
which the pointed-at data is valid. An initial attempt (below) causes this
error:
-```
+```compile_fail
struct Foo<'a, T> {
x: *const T
}
The length of the platform-intrinsic function `simd_shuffle`
wasn't specified. Erroneous code example:
-```
+```compile_fail
+#![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`
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;
}
A platform-specific intrinsic function has the wrong number of type
parameters. Erroneous code example:
-```
+```compile_fail
+#![feature(repr_simd)]
+#![feature(platform_intrinsics)]
+
#[repr(simd)]
struct f64x2(f64, f64);
with yours. Example:
```
+#![feature(repr_simd)]
+#![feature(platform_intrinsics)]
+
#[repr(simd)]
struct f64x2(f64, f64);
An unknown platform-specific intrinsic function was used. Erroneous
code example:
-```
+```compile_fail
+#![feature(repr_simd)]
+#![feature(platform_intrinsics)]
+
#[repr(simd)]
struct i16x8(i16, i16, i16, i16, i16, i16, i16, i16);
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);
Intrinsic argument(s) and/or return value have the wrong type.
Erroneous code example:
-```
+```compile_fail
+#![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);
it the awaited types. Example:
```
+#![feature(repr_simd)]
+#![feature(platform_intrinsics)]
+
#[repr(simd)]
struct i16x8(i16, i16, i16, i16, i16, i16, i16, i16);
Intrinsic argument(s) and/or return value have the wrong type.
Erroneous code example:
-```
+```compile_fail
+#![feature(repr_simd)]
+#![feature(platform_intrinsics)]
+
#[repr(simd)]
struct i16x8(i16, i16, i16, i16, i16, i16, i16, i16);
#[repr(simd)]
it the awaited types. Example:
```
+#![feature(repr_simd)]
+#![feature(platform_intrinsics)]
+
#[repr(simd)]
struct i16x8(i16, i16, i16, i16, i16, i16, i16, i16);
A platform-specific intrinsic function has wrong number of arguments.
Erroneous code example:
-```
+```compile_fail
+#![feature(repr_simd)]
+#![feature(platform_intrinsics)]
+
#[repr(simd)]
struct f64x2(f64, f64);
with yours. Example:
```
+#![feature(repr_simd)]
+#![feature(platform_intrinsics)]
+
#[repr(simd)]
struct f64x2(f64, f64);
The `typeof` keyword is currently reserved but unimplemented.
Erroneous code example:
-```
+```compile_fail
fn main() {
let x: typeof(92) = 92;
}