I'm currently trying to make a Vector
trait, for doing linear algebra. I'd like users to be able to subtract a usize
from a vector or to subtract a vector from a usize
. It's easy to make it possible to subtract a usize
from a Vector
: I make my Vector
trait extend Sub<usize, Output=usize>
. While trying to make it possible to subtract a Vector
from a usize
, I got myself into the following situation:
use std::ops::Sub;
trait Vector {}
fn my_fn<V: Vector>()
where
usize: Sub<V, Output=V>,
{
1usize - 0usize;
}
Surprisingly (to me), I got the following error:
error[E0308]: mismatched types
--> src/lib.rs:9:14
|
9 | 1usize - 0usize;
| ^^^^^^ expected type parameter, found usize
|
= note: expected type `V`
found type `usize`
Some questions that I have:
- What is the name for the thing that I have done, where I seem to have "overridden"
Sub<usize, Output=usize>
withSub<V, Output=V>
? - Is there a way to rewrite the code above so that I can subtract
V
fromusize
to get aV
, but so that I can still subtract ausize
from ausize
to get anotherusize
? - Why doesn't the code above compile? Since
Vector
is in my crate andusize
does not implementVector
, it seems likeusize - usize
andusize - Vector
should be able to coexist peacefully. - Is there another way that I could ensure that my
Vector
trait can be subtracted from ausize
?