What Is the Orphan Rule in Rust?

The orphan rule in Rust prevents implementing an external trait on an external type to avoid conflicting definitions across crates.

The orphan rule in Rust prevents you from implementing a trait on a type if both the trait and the type are defined in an external crate. This rule ensures that different crates cannot accidentally define conflicting implementations for the same combination of trait and type, which would break the compiler's ability to determine which code to run. You can implement a trait on a type only if at least one of them is local to your crate.

// This is ALLOWED: The trait `Summary` is local to this crate.
impl Summary for Vec<String> { ... }

// This is ALLOWED: The type `SocialPost` is local to this crate.
impl std::fmt::Display for SocialPost { ... }

// This is FORBIDDEN: Both `Display` and `Vec` are external.
// impl std::fmt::Display for Vec<String> { ... }