liveBook · Manning

Orphan rule

As explained in Idiomatic Rust

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.

Also explained in

Compare how 2 books explain orphan rule →

Related concepts

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.