I'm trying to redefine the Add
trait so that I could use it as infix operator:
//use std::ops::Add;
trait Add<RHS=Self> {
// type Output;
fn add(self, rhs: RHS) -> Self;
}
fn summ<T: Add>(a: T, b: T) -> T {
a+b
} // doesn't compile
Is it possible to redefine the Add
trait so that it will use the +
operator for add functionality?
It is not possible to redefine any traits. You could create your own trait with the same name and same methods, which is what you have done. However, the operator
+
is tied tostd::ops::Add
, so it wouldn't be useful in this case.In your case, it looks like you just want to specify
Add::Output
to return aT
: