Why doesn't this work:
trait Update {
fn update(&mut self);
}
trait A {}
trait B {}
impl<T: A> Update for T {
fn update(&mut self) {
println!("A")
}
}
impl<U: B> Update for U {
fn update(&mut self) {
println!("B")
}
}
error[E0119]: conflicting implementations of trait `Update`:
--> src/main.rs:14:1
|
8 | impl<T: A> Update for T {
| ----------------------- first implementation here
...
14 | impl<U: B> Update for U {
| ^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation
I would assume it is checked later if types overlap.
What would you expect the output of this program to be?
There is an unstable compiler feature, specialization, which you can enable in nightly builds, which lets you have overlapping instances, and the most "specialized" is used.
But, even with specialization enabled, your example won't work because
A
andB
are completely equivalent so you could never unambiguously pick an instance.As soon as there is an obviously "more specialized" instance, it will compile and work as expected - provided you are using a nightly build of Rust with specialization enabled. For example, if one of the traits is bounded by the other, then it is more specialized, so this would work:
Specifying the implementation method as
default
allows another more specific implementation to define its own version of the method.