How can I refer to a trait's generic parameter in rust?

Viewed 52

for a trait with a type parameter I can reference it in the implementation like so:

impl Mul for Foo {
    type Output = Bar;
    fn mul(self, rhs: self) -> Self::Output {
        unimplemented!()
    }
}

(Self::Output is what I'm referring to, or <Self as Mul>::Output)

is it possible to do the same for a trait's generic parameters? for example:

impl Into<Bar> for Foo {
    fn into(foo: Self) -> <Self as Into>::??? {
        unimplemented!()
    }

}

I'm at a loss for what to replace the question marks with.

1 Answers

No, it's not. This usually isn't useful, anyway, and it definitely can't work the way you've attempted here because a single type can implement Into<T> multiple times with different generic parameters. Assuming this worked with the same syntax as associated types, you'd have to spell out <Self as Into<Bar>>::(something) and then you're naming Bar right there anyway, but with a lot of extra stuff around it for no reason.

Just replace the entire return type with Bar.

Related