E0119 rust type_error ai_generated true

错误[E0119]: trait `Trait` 对类型 `Type` 的实现冲突

error[E0119]: conflicting implementations of trait `Trait` for type `Type`

ID: rust/e0119-conflicting-implementations-of-trait

其他格式: JSON · Markdown 中文 · English
85%修复率
84%置信度
1证据数
2024-03-22首次发现

版本兼容性

版本状态引入弃用备注
rustc 1.75.0 active — — —
rustc 1.78.0 active — — —
rustc 1.80.0 active — — —

根因分析

两个或多个 impl 块为同一类型提供了同一 trait 的实现,或者某个实现与 blanket impl 冲突。

English

Two or more impl blocks provide implementations of the same trait for the same type, or an impl conflicts with a blanket impl.

generic

官方文档

https://doc.rust-lang.org/error_codes/E0119.html

解决方案

  1. Remove the duplicate impl block. Example: keep only one `impl Trait for MyType`.
  2. Use a newtype pattern to create a distinct type. Example: `struct MyWrapper(MyType);` and implement the trait for MyWrapper.
  3. If the conflict is with a blanket impl, add a type constraint or use specialization (if nightly) with `default impl`. Example: `impl<T: std::fmt::Debug> Trait for T { }`.

无效尝试

常见但无效的做法:

  1. 50% 失败

    If both impls are needed for different purposes, removing one will cause missing trait implementation errors elsewhere.

  2. 100% 失败

    Rust doesn't have a lint to suppress conflicting impls; it's a fundamental type system error.

  3. 60% 失败

    If the blanket impl covers the wrapper (e.g., impl<T> Trait for T), the conflict persists.