Conflicting implementations of trait in Rust

浪子不回头ぞ 提交于 2019-11-26 17:23:24

问题


I want to implement a custom trait for &'a str and for integer numbers up to i32, but Rust does not allow me to:

use std::convert::Into;

pub trait UiId {
    fn push(&self);
}

impl<'a> UiId for &'a str {
    fn push(&self) {}
}

impl<T: Into<i32>> UiId for T {
    fn push(&self) {}
}

fn main() {}

This fails to compile with the following error:

error[E0119]: conflicting implementations of trait `UiId` for type `&str`:
  --> src/main.rs:11:1
   |
7  | impl<'a> UiId for &'a str {
   | ------------------------- first implementation here
...
11 | impl<T: Into<i32>> UiId for T {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `&str`
   |
   = note: upstream crates may add new impl of trait `std::convert::From<&str>` for type `i32` in future versions

&'a str does not implement Into<i32>. Is it possible to implement UiId for &'a str and everything that can be converted into i32 without specifying concrete types? How can I do that?


回答1:


The fact that &'a str does not implement Into<i32> is not taken into account, because there is no guarantee that it couldn't be added later. This would then break your code.

So if this were allowed the possible breakage would make it harder to add implementations to library traits.

Unfortunately I couldn't find documentation for that, neither in The Rust Programming Language Book nor in the Reference Manual.

The best I could find is RFC 1023, which says that a crate [...] cannot rely that Type: !Trait holds unless Type or Trait is local.



来源:https://stackoverflow.com/questions/39159226/conflicting-implementations-of-trait-in-rust

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!