What is the orphan rule

The orphan rule prevents implementing external traits on external types to ensure code coherence.

The orphan rule is a Rust coherence rule that prevents you from implementing an external trait on an external type within your crate. You can only implement a trait for a type if either the trait or the type is local to your crate.

// ❌ Error: Both `Display` and `Vec<T>` are external
impl std::fmt::Display for Vec<i32> { }

// ✅ OK: `MyStruct` is local, so we can implement external `Display`
impl std::fmt::Display for MyStruct { }

// ✅ OK: `MyTrait` is local, so we can implement it on external `Vec<T>`
impl MyTrait for Vec<i32> { }