@@ -5,25 +5,6 @@ Erroneous code example:
5
5
``` compile_fail,E0271
6
6
trait Trait { type AssociatedType; }
7
7
8
- fn foo<T>(t: T) where T: Trait<AssociatedType=u32> {
9
- println!("in foo");
10
- }
11
-
12
- impl Trait for i8 { type AssociatedType = &'static str; }
13
-
14
- foo(3_i8);
15
- ```
16
-
17
- This is because of a type mismatch between the associated type of some
18
- trait (e.g., ` T::Bar ` , where ` T ` implements ` trait Quux { type Bar; } ` )
19
- and another type ` U ` that is required to be equal to ` T::Bar ` , but is not.
20
- Examples follow.
21
-
22
- Here is that same example again, with some explanatory comments:
23
-
24
- ``` compile_fail,E0271
25
- trait Trait { type AssociatedType; }
26
-
27
8
fn foo<T>(t: T) where T: Trait<AssociatedType=u32> {
28
9
// ~~~~~~~~ ~~~~~~~~~~~~~~~~~~
29
10
// | |
@@ -56,11 +37,9 @@ foo(3_i8);
56
37
// therefore the type-checker complains with this error code.
57
38
```
58
39
59
- To avoid those issues, you have to make the types match correctly.
60
- So we can fix the previous examples like this:
61
-
40
+ The issue can be resolved by changing the associated type:
41
+ 1 ) in the ` foo ` implementation:
62
42
```
63
- // Basic Example:
64
43
trait Trait { type AssociatedType; }
65
44
66
45
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> {
70
49
impl Trait for i8 { type AssociatedType = &'static str; }
71
50
72
51
foo(3_i8);
52
+ ```
73
53
74
- // For-Loop Example:
75
- let vs = vec![1, 2, 3, 4];
76
- for v in &vs {
77
- match v {
78
- &1 => {}
79
- _ => {}
80
- }
54
+ 2 ) in the ` Trait ` implementation for ` i8 ` :
55
+ ```
56
+ trait Trait { type AssociatedType; }
57
+
58
+ fn foo<T>(t: T) where T: Trait<AssociatedType = u32> {
59
+ println!("in foo");
81
60
}
61
+
62
+ impl Trait for i8 { type AssociatedType = u32; }
63
+
64
+ foo(3_i8);
82
65
```
0 commit comments