"ambiguous associated type" even though it isn't ambiguous

MWE

pub trait Trait {
    type Assoc;
}

struct Foo;

impl Trait for Foo {
    type Assoc = String;
}

fn foo() -> Foo::Assoc {
    Foo::Assoc::from("abc")

but the compiler tells me "use fully-qualified syntax: <Foo as Trait>::Assoc". What am I missing? Surely we don't have to use fully-qualified syntax every time we refer to an associated type?

That's simply a bug. Low-priority AFAICS.

They perhaps don't want to go looking at every implemented trait to be able to interpret the function signature, say.

You could use a type alias.

3 Likes

Oh damn. Sad, it appears that both this issue and the long-standing "inherent associated types" (the lack of which I was trying to solved with the snippet above) are blocked by more or less the same thing x)


In fact this is what I wish I could write

struct Foo<T> (T);

impl<T> Foo<T> {
    type Assoc = SomeComplicatedTypeInvolving<T>;
}

Unfortunately neither this, nor the "auxiliary trait" approach in the 1st post, nor the "type alias" suggestion work :frowning:

Actually yes, this does work for my purposes! Rust Playground

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.