[[外部トレイト]]の[[Displayトレイト]]は、[[外部の型]] [[String]] に実装できない。[[コヒーレンス]]や[[オーファンルール]]の特性の一部。
```rust
use std::fmt::{Display, Formatter};
impl Display for String { // ここにエラーが出る
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self)
}
}
fn main() {
println!("{}", String::from("hoge"));
}
```
```console
$ cargo run
error[E0117]: only traits defined in the current crate can be implemented for types defined outside of the crate
--> src\main.rs:3:1
|
3 | impl Display for String {
| ^^^^^^^^^^^^^^^^^------
| | |
| | `String` is not defined in the current crate
| impl doesn't use only types from inside the current crate
|
= note: define and implement a trait or new type instead
```
## MOC
- 📒**関連**
- [[Rustの外部トレイトは内部の型に実装できる]]
- [[Rustの内部トレイトは外部の型に実装できる]]