Conflicting implementations of Mul trait when using borrow types


I am trying to implement vector multiplication elementwise and by a scalar, so I wrote four implementations, two that take owned values and two that took borrowed values for each elementwise and scalar multiplication. offending code in pastebin (lines 40 and 51 are the bad implementations)

The problem above also happens when I try to implement for Div.

the exact error is:
error[E0119]: conflicting implementations of trait std::ops::Mul<&vector::Vector<_>> for type &vector::Vector<_>:


What’s your question exactly? :slight_smile:

For the reference case, you can define a newtype Scalar<U>(U) and implement the multiplication for it rather than a generic U.

The reason why there’s no overlap in the owned case (is that your question?) is because the compiler knows that in the following declaration:

impl<T, U, O> Mul<U> for Vector<T>
    where T: Clone + Copy + Mul<U, Output = O>,
          U: Clone + Copy,
          O: Clone + Copy 

U cannot be Vector<T> because you put a Copy constraint on U - Vector<T> cannot be Copy.

In the reference case, it can be because a &'a Vector<T> is Copy (i.e. an immutable reference is always Copy).

By the way, you don’t need Clone + Copy trait bounds - Copy implies Clone.