1 This is because of a type mismatch between the associated type of some
2 trait (e.g., `T::Bar`, where `T` implements `trait Quux { type Bar; }`)
3 and another type `U` that is required to be equal to `T::Bar`, but is not.
6 Here is a basic example:
9 trait Trait { type AssociatedType; }
11 fn foo<T>(t: T) where T: Trait<AssociatedType=u32> {
15 impl Trait for i8 { type AssociatedType = &'static str; }
20 Here is that same example again, with some explanatory comments:
23 trait Trait { type AssociatedType; }
25 fn foo<T>(t: T) where T: Trait<AssociatedType=u32> {
26 // ~~~~~~~~ ~~~~~~~~~~~~~~~~~~
28 // This says `foo` can |
29 // only be used with |
31 // implements `Trait`. |
33 // This says not only must
34 // `T` be an impl of `Trait`
35 // but also that the impl
36 // must assign the type `u32`
37 // to the associated type.
41 impl Trait for i8 { type AssociatedType = &'static str; }
42 //~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
47 // ... but it is an implementation
48 // that assigns `&'static str` to
49 // the associated type.
52 // Here, we invoke `foo` with an `i8`, which does not satisfy
53 // the constraint `<i8 as Trait>::AssociatedType=u32`, and
54 // therefore the type-checker complains with this error code.
57 To avoid those issues, you have to make the types match correctly.
58 So we can fix the previous examples like this:
62 trait Trait { type AssociatedType; }
64 fn foo<T>(t: T) where T: Trait<AssociatedType = &'static str> {
68 impl Trait for i8 { type AssociatedType = &'static str; }
73 let vs = vec![1, 2, 3, 4];