lotsoftools

Understanding Rust Error E0212

Overview of Rust Error E0212

Rust Error E0212 occurs when you attempt to use the associated type of a trait with uninferred generic parameters. This puts the compiler in a position where it cannot determine the specific type or lifetime that should be used.

Example of Erroneous Code:

```
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!
```

In this erroneous code example, 'x needs to be instantiated, but the lifetime to instantiate it with is not clear. Thus, E0212 is raised.

Fixing Rust Error E0212

To resolve Rust Error E0212, you should spell out the precise lifetimes involved. Doing so provides the compiler with enough information to work with.

Example of Corrected Code:

```
pub trait Foo<T> {
    type A;

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

fn foo3<I : for<'x> Foo<&'x isize>>(
    x: <I as Foo<&isize>>::A) {} // ok!

fn foo4<'a, I : for<'x> Foo<&'x isize>>(
    x: <I as Foo<&'a isize>>::A) {} // ok!
```

Now the code correctly specifies the lifetimes, preventing Rust Error E0212 from being raised.