Why not "Conflicting implementations"?

This code happily compiles:

trait MyTrait<T> {}
impl<T> MyTrait<T> for T {}
impl<T> MyTrait<T> for Option<T> {}

I was expecting it to not compile with the "Conflicting trait implementations" error.

This code is very slightly different but does actually not compile:

trait MyTrait {}
impl<T> MyTrait for T {}
impl<T> MyTrait for Option<T> {}

What rust rules are involved here? I've briefly read the chapter about traits in the Rust Book, but I did not find anything about this.

Since the first trait is generic, you can consider it like a trait constructor, and the concrete traits implemented by the blanket impls are essentially different traits. In the first blanket impls, if you substitute Option<T> in, then the implemented trait is MyTrait<Option<T>> which is a different trait from MyTrait<T>.

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.