#[cfg(test)]
mod t {
use std::iter::Map;
use std::ops::{Mul, Range};
impl<I: Iterator, F> Mul for I {
type Output<F> = Map<I, F>;
fn mul(self, mut rhs: Self) -> Self::Output<F> {
self.map(|x| x * rhs.next().unwrap())
}
}
#[test]
fn io() {
let m = vec![1, 2, 3] * vec![2, 3, 4];
}
}
Of course the error comes from the Function type, which is not really generic, but I don't know how to do ? error[E0207]: the type parameter Fis not constrained by the impl trait, self type, or predicates
continuing the experimentation, I have something different here
#[cfg(test)]
mod t {
use std::iter::Map;
use std::ops::{Mul, Range};
impl<I: Iterator> Mul for I {
type Output = Self;
fn mul(self, mut rhs: Self) -> Self::Output {
self.zip(rhs).map(|(a, b)| a * b)
}
}
#[test]
fn io() {
let m = vec![1, 2, 3] * vec![2, 3, 4];
}
}
error[E0119]: conflicting implementations of trait `std::ops::Mul` for type `u32`:
--> src\traits\ops.rs:10:5
|
10 | impl<I: Iterator> Mul for I {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: conflicting implementation in crate `core`:
- impl std::ops::Mul for u32;
= note: upstream crates may add a new impl of trait `std::iter::Iterator` for type `u32` in future versions
error[E0210]: type parameter `I` must be used as the type parameter for some local type (e.g., `MyStruct<I>`)
--> src\traits\ops.rs:10:10
|
10 | impl<I: Iterator> Mul for I {
| ^ type parameter `I` must be used as the type parameter for some local type
|
= note: implementing a foreign trait is only possible if at least one of the types for which is it implemented is local
= note: only traits defined in the current crate can be implemented for a type parameter
Rustc here complains about the fact that another crate could also have implemented the trait if it allows this implementation. Because this would cause big problems, you are only allowed to implement a trait if either the trait or the type you implement it on is defined in the current crate. (the rules are a bit more complex, but this is the basic rule) For the exact rules see RFC1023 and RFC2451.