@@ -5,25 +5,6 @@ Erroneous code example:
55``` compile_fail,E0271
66trait Trait { type AssociatedType; }
77
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-
278fn foo<T>(t: T) where T: Trait<AssociatedType=u32> {
289// ~~~~~~~~ ~~~~~~~~~~~~~~~~~~
2910// | |
@@ -56,11 +37,9 @@ foo(3_i8);
5637// therefore the type-checker complains with this error code.
5738```
5839
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:
6242```
63- // Basic Example:
6443trait Trait { type AssociatedType; }
6544
6645fn 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> {
7049impl Trait for i8 { type AssociatedType = &'static str; }
7150
7251foo(3_i8);
52+ ```
7353
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");
8160}
61+
62+ impl Trait for i8 { type AssociatedType = u32; }
63+
64+ foo(3_i8);
8265```
0 commit comments