Private trait and E0119?

Is Rust has thing like “private trait”?
I mean trait defined wihtout pub keyword is private?

Code bellow gives compilation error:

error[E0119]: conflicting implementations of trait `MyFrom<i64>` for type `std::option::Option<&_>`:
  --> src/
16 | impl<T: Foo> MyFrom<i64> for Option<T> {
   | -------------------------------------- first implementation here
22 | impl<T: Foo> MyFrom<i64> for Option<&T> {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `std::option::Option<&_>`
   = note: downstream crates may implement trait `Foo` for type `&_`

But this is not public trait, and this is exe , how can " downstream crates may implement trait Foo for type &_" ? It is impossible to import trait from exe crate, and even if possible this is not pub trait,
so rustc doesn’t support private traits?

trait MyFrom<T> {
    fn my_from(_: T) -> Self;

trait Foo {}

impl<T: Foo> MyFrom<i64> for Option<T> {
    fn my_from(x: i64) -> Self {

impl<T: Foo> MyFrom<i64> for Option<&T> {
    fn my_from(x: i64) -> Self {

fn main() {

Yes, this my post too, I cross-posted, because of not sure how big overlap of audience of these sites.

I thought so. The reason why I do that (quiet often) is when somebody searches for your topic and gets here, he may find the answer on Stackoverflow.
Have a look at my “about me”

If I post a Stackoverflow post under your question please don’t be offended :slight_smile:

1 Like

You got reply on SO, but it seems its not clear to you. This doesn’t compile, because &T is a valid type on its own (references are types and matches everywhere types matches - its not something different). If you would have no special implementation for reference type, it would be fine - reference type would use generic implementation, but in this very case, it doesn’t know which implementation to choose. Rust doesn’t have things like “Take better match” (yet - specializations are comming, they may change a little here), it always expect to have one match to use.

Also this possible matching is not checked lately when generic is instanced (like in C++), it checked on definition. It is good, because if you are creating crate, then in “late check” way, you would don’t know you have overlapping implementation unless someone report bug for your crate.

Are you sure you cannot create common implementation for MyFrom? Maybe using traits like Borrow or AsRef?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.