我正在尝试创建一个特征并为所有非引用类型提供一个实现,并为所有引用类型提供另一个实现.
这无法编译:
trait Foo {}
impl<T> Foo for T {}
impl<'a, T> Foo for &'a mut T {}
Run Code Online (Sandbox Code Playgroud)
这失败了,错误
error[E0119]: conflicting implementations of trait `Foo` for type `&mut _`:
--> src/main.rs:3:1
|
2 | impl<T> Foo for T {}
| -------------------- first implementation here
3 | impl<'a, T> Foo for &'a mut T {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `&mut _`
Run Code Online (Sandbox Code Playgroud)
奇怪的是,这有效:
trait Bar {}
impl<T> Bar for T
where
T: Clone,
{}
impl<'a, T> Bar for &'a mut T …Run Code Online (Sandbox Code Playgroud) rust ×1