masion
masion

Reputation: 1

Cannot use the associated type of a trait with uninferred generic parameters?

This example is from rust's error-index, whose explanation I still can't understand why this is the case

#![allow(unused)]
fn main() {
    pub trait Foo<T> {
        type A;

        fn get(&self, t: T) -> Self::A;
    }

    fn foo2<I: for<'x> Foo<&'x isize>>(field: I::A) {} // error!
}

Upvotes: 0

Views: 176

Answers (1)

Chayim Friedman
Chayim Friedman

Reputation: 71595

I does not implement Foo once in foo2(). It implements it multiple times, once for each lifetime of 'x.

Because of that, there is also not a single value for <I as Foo>::A. There are multiple, one for each instantiation of 'x.

When you want to specify I::A, you need to tell the compiler which I::A - that is, which lifetime to bind 'x with.

Upvotes: 2

Related Questions