简体   繁体   中英

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

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM