In Rust, why does rustc accept `impl From<Celsius> for f64` and `impl IntoIterator for &Readings`, yet reject `impl fmt::Display for Vec<Celsius>` with E0117?
answer
- the trait's type arguments count too
- first local type, nothing uncovered before
- a few type constructors are fundamental
- &T, &mut T, Box<T>, Pin<T>
basics
~20 sThe orphan rule needs one local type anywhere in the impl header, with no uncovered type parameter before it. Celsius as From's argument qualifies; &Readings is local because & is fundamental; Vec<Celsius> is a foreign Vec.
solid answer
~40 sThe full orphan rule looks at every type in the header, not just `Self`. For `impl<P..> Trait<T1..Tn> for T0` with a foreign trait, at least one of `T0..Tn` must be local, and no uncovered type parameter may appear before the first local one. In `impl From<Celsius> for f64`, `T0` is `f64` and `T1` is the local `Celsius`, with no type parameters at all, so it passes. `&` is a **fundamental** type constructor, as are `&mut`, `Box` and `Pin`, so `&Readings` counts as local, which is why `impl IntoIterator for &Readings` is idiomatic. `Vec` is not fundamental: a local type inside it does not make `Vec<Celsius>` local, so trait and type are both foreign and rustc reports E0117. Generic impls add E0210: `impl<T: Debug> Display for T` leaves `T` uncovered.
code
rust · 35 linesuse std::fmt;
struct Celsius(f64);
struct Readings(Vec<Celsius>);
// Foreign trait, foreign Self type, but the trait's type argument is local.
impl From<Celsius> for f64 {
fn from(c: Celsius) -> f64 {
c.0
}
}
// Foreign trait for &Readings: & is a fundamental type, so &Readings counts as local.
impl<'a> IntoIterator for &'a Readings {
type Item = &'a Celsius;
type IntoIter = std::slice::Iter<'a, Celsius>;
fn into_iter(self) -> Self::IntoIter {
self.0.iter()
}
}
impl fmt::Display for Celsius {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:.1}°C", self.0)
}
}
fn main() {
let r = Readings(vec![Celsius(21.5), Celsius(23.0)]);
for c in &r {
print!("{c} ");
}
let raw: f64 = Celsius(19.25).into();
println!("| {raw}");
}go deeper
Remember that a local type may appear as a trait's type argument, as in From<Celsius> for f64, and that still satisfies the orphan rule.
Explain why Vec<Celsius> stays foreign while &Readings is local, naming the fundamental constructors: &, &mut, Box and Pin.
Walk the full header rule, the first local type with nothing uncovered before it, and tell E0117 from E0210, including why Box<Local> can still hit E0119.
Relate the ordering rule to its purpose: it keeps two crates from writing overlapping generic impls blind, which is what makes adding impls across the ecosystem safe.
## The short rule and the real one The one-line orphan rule, "you need the trait or the type", is a simplification. The Rust Reference states it precisely. Given an impl header `impl<P1..=Pn> Trait<T1..=Tn> for T0` the impl is valid only if at least one of these holds: 1. `Trait` is a **local trait**, defined in the current crate; or 2. at least one of the types `T0..=Tn` is a **local type** (call the first one `Ti`), **and** no **uncovered** type parameter from `P1..=Pn` appears in `T0..Ti` before it. The types are `Self` (`T0`) followed by the trait's own type arguments (`T1` onward). A type parameter is **uncovered** when it appears bare, as `T`, rather than as an argument inside another type, as in `Vec<T>`. ## Case by case | impl header | `T0` | first local type | result | |---|---|---|---| | `impl From<Celsius> for f64` | `f64` (foreign) | `T1 = Celsius` | accepted | | `impl<'a> IntoIterator for &'a Readings` | `&Readings` | `T0`, via fundamental `&` | accepted | | `impl fmt::Display for Vec<Celsius>` | `Vec<Celsius>` (foreign) | none | E0117 | | `impl<T: Debug> fmt::Display for T` | `T` (uncovered) | none | E0210 | | `impl<T> From<Celsius> for Vec<T>` | `Vec<T>`, `T` covered | `T1 = Celsius` | accepted | | `impl<T> From<T> for Vec<Celsius>` | `Vec<Celsius>` (foreign) | none; `T1 = T` uncovered | E0210 | What the rows show: - **A local type as a trait argument is enough.** `From<Celsius> for f64` and `PartialEq<Celsius> for f64` are ordinary. Only your crate can name `Celsius`, so no other crate can write the same pair. - **Wrapping does not localise.** The Reference glossary says locality is not affected by type arguments: `struct Foo` is local, `Vec<Foo>` is not, while `LocalType<ForeignType>` is local. Type aliases do not change locality either. - **Covered parameters are fine.** In `impl<T> From<Celsius> for Vec<T>` the `T` sits inside `Vec`, so it is covered, and the first local type, `Celsius`, has nothing uncovered before it. - **A bare `T` with no local type ahead of it is fatal.** `impl<T: Debug> fmt::Display for T` would implement `Display` for every `Debug` type in every crate; rustc stops it with E0210, "type parameter `T` must be used as an argument to some local type". ## Fundamental type constructors A few constructors are **fundamental**: `&`, `&mut`, `Box` and `Pin`. For coherence, whenever `T` is local, `&T`, `&mut T`, `Box<T>` and `Pin<T>` are local too, and the `T` inside them does not count as covered. That is what makes these idiomatic impls legal: - `impl<'a> IntoIterator for &'a Readings`, so `for c in &readings` works; - `impl<'a> IntoIterator for &'a mut Readings` for mutable iteration; - foreign traits for `Box<YourType>` where no existing impl covers it. The flip side is that std may not later add a blanket impl over these constructors without breaking code, and it already has forwarding impls such as `impl<T: Display + ?Sized> Display for Box<T>`. So `impl fmt::Display for Box<Celsius>` passes the orphan check yet fails with E0119 as soon as `Celsius` implements `Display`, because alloc's forwarding impl already covers it. Fundamental status only moves locality to the inner type: `Box<SystemTime>` is still foreign and gets E0117. `Rc`, `Arc`, `Option` and `Vec` are not fundamental, so `Rc<Celsius>` and `Option<Celsius>` are foreign types. ## Reading the two diagnostics - **E0117** fires when no local type appears among `T0..Tn` and no bare type parameter is involved; its note reads "impl doesn't have any local type before any uncovered type parameters" and suggests a new trait or type. - **E0210** fires when a type parameter appears uncovered where the rule forbids it; its notes say a foreign trait can be implemented only if one of the types is local, and only local traits can be implemented for a type parameter. ## Why the rule is shaped this way The ordering condition keeps two crates from writing overlapping generic impls without seeing each other. If `impl<T> From<T> for Vec<Celsius>` were allowed, another crate could legally write `impl<U> From<Kelvin> for Vec<U>`, and both would apply to `Vec<Celsius>: From<Kelvin>`. Requiring a local type to come first, with nothing uncovered ahead of it, rules out such cross-crate overlaps. That is the price of coherence: a few impls that look harmless must go through a local wrapper type.
- Why does `impl fmt::Display for Box<Celsius>` compile in one crate and fail in another?`Box` is fundamental, so `Box<Celsius>` counts as local and the orphan check passes. But alloc already has `impl<T: Display + ?Sized> Display for Box<T>`. If `Celsius` implements `Display`, the two impls overlap and rustc reports E0119; without a `Display` impl for `Celsius`, there is no overlap and it compiles.
- Why is `impl<T> From<T> for Vec<Celsius>` rejected when `impl<T> From<Celsius> for Vec<T>` is accepted?In the second, `T` is covered by `Vec` and the first local type, `Celsius` as `T1`, has nothing uncovered before it. In the first, `Vec<Celsius>` is not local and `T1` is a bare `T`, so there is no local type at all and `T` is uncovered: E0210.
- Only the Self type matters for the orphan rule; trait arguments are ignored.
- Vec<MyType> and Option<MyType> count as local because they contain a local type.
- impl From<MyType> for f64 breaks the orphan rule because f64 is foreign.
- Rc<MyType> counts as local just like Box<MyType>.
- A blanket impl<T: Debug> Display for T is fine because the trait bound limits it.