Update E0271.md
remove references to non existing code, expand solution suggestions remove unneeded code in solution
This commit is contained in:
parent
0820e54a8a
commit
7e68b7d10a
@ -5,25 +5,6 @@ Erroneous code example:
|
||||
```compile_fail,E0271
|
||||
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);
|
||||
```
|
||||
|
||||
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 that same example again, with some explanatory comments:
|
||||
|
||||
```compile_fail,E0271
|
||||
trait Trait { type AssociatedType; }
|
||||
|
||||
fn foo<T>(t: T) where T: Trait<AssociatedType=u32> {
|
||||
// ~~~~~~~~ ~~~~~~~~~~~~~~~~~~
|
||||
// | |
|
||||
@ -56,11 +37,9 @@ foo(3_i8);
|
||||
// therefore the type-checker complains with this error code.
|
||||
```
|
||||
|
||||
To avoid those issues, you have to make the types match correctly.
|
||||
So we can fix the previous examples like this:
|
||||
|
||||
The issue can be resolved by changing the associated type:
|
||||
1) in the `foo` implementation:
|
||||
```
|
||||
// Basic Example:
|
||||
trait Trait { type AssociatedType; }
|
||||
|
||||
fn foo<T>(t: T) where T: Trait<AssociatedType = &'static str> {
|
||||
@ -70,13 +49,17 @@ fn foo<T>(t: T) where T: Trait<AssociatedType = &'static str> {
|
||||
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 => {}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
2) in the `Trait` implementation for `i8`:
|
||||
```
|
||||
trait Trait { type AssociatedType; }
|
||||
|
||||
fn foo<T>(t: T) where T: Trait<AssociatedType = u32> {
|
||||
println!("in foo");
|
||||
}
|
||||
|
||||
impl Trait for i8 { type AssociatedType = u32; }
|
||||
|
||||
foo(3_i8);
|
||||
```
|
||||
|
Loading…
x
Reference in New Issue
Block a user