Getting the type of Add implementation pin associated with it taking into account the LHS and RHS types

In Rust, is there a type-level way to invoke the Add implementation using the LHS ( Self ) and RHS types to use its Output type (say, returning the type of a generic function)?

+5
source share
1 answer

Yes, although it looks like black magic.

You need to combine 3 bits of syntax:

  • Type attribute accessibility is available through <Type as Trait>
  • specifying RHS just requires passing it as an Add<???> parameter
  • and finally, getting the associated attribute type just requires using Trait::OutputType (which may be ambiguous)

Combining 3 together, we get <Self as Add<RhsType>>::Output .

+8
source

Source: https://habr.com/ru/post/1258516/


All Articles