Is it possible to implement the std::ops::Sub
trait for slices in Rust? And if so how would I do that?
I tried the following:
use std::ops::Sub;
impl <'a, 'b, T> Sub<&'a [T]> for &'b [T] {
type Output = Vec<T>;
fn sub(self, rhs: &[T]) -> Vec<T> {
difference(self, rhs)
}
}
with difference
having the signature:
difference<T: PartialEq + Copy>(xs: &[T], ys: &[T]) -> Vec<T>
compiling it results in the following error message:
error: type parameter `T` must be used as the type parameter for some local type
(e.g. `MyStruct<T>`); only traits defined in the current crate
can be implemented for a type parameter [E0210]
impl <'a, 'b, T> Sub<&'a [T]> for &'b [T] {
^
running then rust --explain E0210
to get further information I can kinda understand the problem, but I am confused about how the syntax would look like when using the struct MyType<T>(T);
workaround described in the detailed explanation.
In difference to the question found How do I implement a trait I don't own for a type I don't own? my problem is I do not want this for vector types, but for slice types for which the syntax seems to be different enough that this answer is not really helpful.