Error E0119 with the implementation of common symptoms

I have an enumeration:

enum Field {
    Str(String),
    Integer(i64),
}

I want to do:

impl From<String> for Field {
    fn from(s: String) -> Field {
        Field::Str(s)
    }
}

impl<I> From<I> for Field where I: Into<i64> + Copy {
    fn from(i: I) -> Field {
        Field::Integer(Into::<i64>::into(i))
    }
}

This code has an error:

error[E0119]: conflicting implementations of trait
`std::convert::From<std::string::String>` for type `Field`:
--> <anon>:12:5
   |
6  |          impl From<String> for Field {
   |  ________- starting here...
7  | |       fn from(s: String) -> Field {
8  | |         Field::Str(s)
9  | |       }
10 | |     }
   | |_____- ...ending here: first implementation here
11 | 
12 |       impl<I> From<I> for Field where I: Into<i64> + Copy {
   |  _____^ starting here...
13 | |       fn from(i: I) -> Field {
14 | |         Field::Integer(Into::<i64>::into(i))
15 | |       }
16 | |     }
   | |_____^ ...ending here: conflicting implementation for `Field`

Stringnot a developer Into<i64>, so why is the error happening E0119?

+4
source share
1 answer

TL DR: whereArticles do not count.


The essence of the problem is that conflict detection is based only on a template: it does not take into account proposals where.

Task 3 times:

  • Deciding whether elements allow whereoverlapping or not,
  • Determining which offer whereis more specialized than the other is quite difficult (upcoming specialization),
  • , .

, . , :

  • Copy,
  • Rust impl Into<i64> for &str.

, , ! !

, . :

  • impl, (),
  • .

.


: Rust explain <code> , E0119. .

+3
source

Source: https://habr.com/ru/post/1672211/


All Articles