Skip to content

Commit 07e32fc

Browse files
authored
Merge pull request #1755 from TmSalviano/orphan_rule_rationale
Add: orphan rule rationale.
2 parents eafe90c + e496215 commit 07e32fc

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/items/implementations.md

+7
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,13 @@ be instantiable with the same set of types for the input type parameters. -->
173173
r[items.impl.trait.orphan-rule]
174174
#### Orphan rules
175175

176+
r[items.impl.trait.orphan-rule.intro]
177+
The *orphan rule* states that a trait implementation is only allowed if either the trait or at least one of the types in the implementation is defined in the current crate. It prevents conflicting trait implementations across different crates and is key to ensuring coherence.
178+
179+
An orphan implementation is one that implements a foreign trait for a foreign type. If these were freely allowed, two crates could implement the same trait for the same type in incompatible ways, creating a situation where adding or updating a dependency could break compilation due to conflicting implementations.
180+
181+
The orphan rule enables library authors to add new implementations to their traits without fear that they'll break downstream code. Without these restrictions, a library couldn't add an implementation like `impl<T: Display> MyTrait for T` without potentially conflicting with downstream implementations.
182+
176183
r[items.impl.trait.orphan-rule.general]
177184
Given `impl<P1..=Pn> Trait<T1..=Tn> for T0`, an `impl` is valid only if at
178185
least one of the following is true:

0 commit comments

Comments
 (0)