error[E0210]: type parameter `T` must be used as the type parameter for some local type (e.g., `MyStruct<T>`)

孤児ルール』と型パラメタが絡んだエラー。

原因

孤児ルールは、クレート外部の型やトレイトに厳しい。

一方、型パラメタの対象にはクレート外部の型もなりうる。

そのため、型パラメタが孤児ルールのきっかけになる事がある。

サンプル

以下では、外部の型になりうる T に、外部のトレイト Display を実装している。


use std::fmt::{self, Display, Formatter};

impl<T> Display for T {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "")
    }
}

error[E0210]: type parameter `T` must be used as the type parameter for some local type (e.g., `MyStruct<T>`)
 --> src\lib.rs:3:6
  |
3 | impl<T> Display for T {
  |      ^ type parameter `T` must be used as the type parameter for some local type
  |
  = note: implementing a foreign trait is only possible if at least one of the types for which it is implemented is local
  = note: only traits defined in the current crate can be implemented for a type parameter