Rust's orphan rule restricts trait implementations so that an implementation must involve a locally defined trait or type, subject to its coherence rules.
Rust’s orphan rule limits trait implementations: a crate cannot implement an external trait for an external type. section 2.2.6
The rule leaves several options:
Implement a local trait for any type, including one from an external crate. section 2.2.6
Implement an external trait for external types when it has multiple type parameters and one covered parameter is local. section 2.2.6
Use a wrapper struct or extension trait as a workaround. section 2.2.6
How Idiomatic Rust covers it
How deep it goes
Defines the local/external boundary and a covered-type exception, while directing readers to the language reference for details.
Examples
Shows `MyTrait` implemented for `MyStruct1` and `MyStruct2`.
What you can do after
Apply the rule by choosing a permitted implementation or using a wrapper struct or extension trait.
How it connects
Connects trait implementations with local traits, external traits, local types, wrapper structs, and extension traits.
Write Powerful Rust MacrosAn orphan rule restricts trait implementations so that at least the trait or the implemented type is local to the defining crate.
newtype idiomaddressed by · The newtype idiom defines a distinct type by wrapping an existing type, typically preserving its representation while enabling type-specific distinctions or behavior.
type aliascontrasts with · A type alias is a declaration that gives an existing type an alternative name without creating a distinct type or representation.
traitcontrasts with · A trait is a type-level construct that defines reusable behavior or members that types can implement or compose.
This explainer was generated by Manning's knowledge graph from the text of Idiomatic Rust. The section links point to the parts of the book it draws on.