liveBook · Manning

Orphan rule

Rust's orphan rule restricts trait implementations so that either the trait or the implementing type must be local to the defining crate.

How 2 Manning books explain it

Write Powerful Rust Macros

An orphan rule restricts trait implementations so that at least the trait or the implemented type is local to the defining crate.

How it is used
Uses local newtypes to add trait implementations when the desired trait or wrapped type is nonlocal.
How deep it goes
Explains the locality requirement, conflicting-implementation rationale, and newtype workaround, but does not detail compiler coherence internals.
Examples
Uses a hypothetical Clone implementation for String and a MyString newtype as the concrete workaround.
What you can do after
Apply it via a local newtype to implement traits for otherwise nonlocal types.

Read how Write Powerful Rust Macros explains orphan rule →

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.

How it is used
Constrains trait implementations in Rust according to local and external ownership of traits and types.
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.

Read how Idiomatic Rust explains orphan rule →

Related concepts

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.