实现多种Borrow <T>类型的一个特征会导致实现冲突

时间:2019-06-22 11:14:04

标签: rust traits

use std::borrow::Borrow;

trait Foo {
    fn bar();
}

struct S {}

impl <B: Borrow<S>> Foo for B {
    fn bar(){
        print!("bar");
    }
}

struct D {}

impl <B: Borrow<D>> Foo for B {
    fn bar(){
        print!("bar")
    }
}

fn main() {}

上面的代码会导致编译器错误:

   Compiling playground v0.0.1 (/playground)
error[E0119]: conflicting implementations of trait `Foo`:
  --> src/main.rs:17:5
   |
9  |     impl <B: Borrow<S>> Foo for B {
   |     ----------------------------- first implementation here
...
17 |     impl <B: Borrow<D>> Foo for B {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation

我在这里Do I have to implement a trait twice when implementing it for both reference and non-reference types?问了一个问题

只有一个实现此特征的结构时,可接受的答案会很好。

如果我想同时为FooBorrow<S>实现特征Borrow<D>,怎么办?

0 个答案:

没有答案
相关问题