Rust
E0117 Orphan Rule Fix
E0117 is the orphan rule error: an impl reaches for a trait and a type that both belong to other crates, and the compiler refuses to let this crate speak for either of them. This guide separates the eleven shapes that produce it, matches each one to the repair that fits it - a local newtype wrapper, a local type in the trait's parameters, or a local trait - and draws the line against the neighboring impl errors that look similar but are not this one.
Written November 25, 2026 · Tested with Rust 1.99.1 · 40 min read
- The rule underneath: a local trait, or a local type before any uncovered parameter
- How to read the diagnostic
- The first failure: Display for Vec
- The second failure: Display for a fixed-size array
Written November 25, 2026
Tested with Rust 1.99.1
What this guide is
Error E0117 is the orphan rule error. The error codes index states it in one line - only traits defined in the current crate can be implemented for arbitrary types - and gives one example: impl Drop for u32 {}, an impl of a standard-library trait for a primitive type. Every failure in this guide has that same skeleton. The trait belongs to another crate, the type it is implemented for belongs to another crate, and any type parameters the trait takes are foreign too - so the impl belongs to no crate that is present to own it, and the compiler refuses it.
The rule exists for coherence. The Book puts it plainly: without the rule, two crates could implement the same trait for the same type, and Rust would not know which implementation to use. The orphan rule guarantees that an impl is always owned by the crate that defined the trait or by the crate that defined the type, so exactly one crate is responsible for it and no downstream crate can be broken by another crate adding an impl of its own.
The repairs fall into three groups. Wrap the foreign type in a local newtype and implement the trait for the wrapper - the repair that always applies, because the wrapper is a local type. Give the trait a local type somewhere in the impl - as the implementing type, or as one of the trait's type parameters - which is the repair when the impl's shape already has a place for one. Or define a local trait and implement that instead, which is the repair when the foreign trait itself is not essential. Which repair fits depends on what the impl is for, and the diagnostic names the type that is not local - so reading it comes first.
Build every program in this guide with rustc --edition 2021. In the compiler transcripts below, the file names are the bare names shown on the command line.
The rule underneath: a local trait, or a local type before any uncovered parameter
The Reference states the orphan rule as a condition on the whole impl: a trait implementation is allowed only if the trait is defined in the current crate, or at least one of the types in the implementation is defined in the current crate. Its formal version considers impl<P1..=Pn> Trait<T1..=Tn> for T0 - the implementing type first, then the trait's parameters in order. The impl is valid if the trait is local, or if, scanning those types in order, some type Ti is local and no uncovered type parameter appears in the types before it. A type parameter is uncovered when it appears bare; a parameter sitting inside another type, such as the T in Vec<T>, is covered and does not break the scan - but coverage is not locality, and a covered parameter does not make the type containing it local either.
One class of types bends the locality half of that scan. The Reference calls them fundamental types: for coherence, the T in Box<T> is not considered covered, and Box<LocalType> is considered local. References behave the same way. So an impl for Box<Local> or &Local is an impl for a local type, while an impl for Box<i32> is not - the box is transparent, and what decides is the type inside it. Ordinary generic types such as Vec and Option are not fundamental: Vec<Local> is a foreign type that happens to mention a local one, and it fails the rule exactly as Vec<i32> does. Both halves of that contrast are families below.
Two consequences follow, and both appear below. First, position inside the crate does not matter: the rule is stated per crate, not per module or per function, so moving an impl into a module or a function body changes nothing. Second, the check is about the impl as written, not about whether the impl would clash with an existing one: an impl of a trait that the standard library already implements for the same type is still reported as E0117 when it is an orphan, because the orphan check is the one it fails.
How to read the diagnostic
Every family in this guide prints error E0117 with the span on the impl header, a label naming what is not local, and three notes. The header's ending varies with the implementing type: most families read "only traits defined in the current crate can be implemented for types defined outside of the crate", the array, tuple, and slice families read "only traits defined in the current crate can be implemented for arbitrary types", and the Drop trap at the end reads "only traits defined in the current crate can be implemented for primitive types". The first family's, in full:
$ rustc --edition 2021 fail_display_vec.rs -o fail_display_vec
error[E0117]: only traits defined in the current crate can be implemented for types defined outside of the crate
--> fail_display_vec.rs:2:1
|
2 | impl fmt::Display for Vec<i32> {
| ^^^^^^^^^^^^^^^^^^^^^^--------
| |
| `Vec` is not defined in the current crate
|
= note: impl doesn't have any local type before any uncovered type parameters
= note: for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules
= note: define and implement a trait or new type instead
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0117`.
Read it in order. The header states the rule, not the specific defect - the defect is in the label under the span, which underlines the implementing type and says "Vec is not defined in the current crate". The first note restates the formal condition: "impl doesn't have any local type before any uncovered type parameters". The second note points at the Reference's orphan-rules section, and the third names the two repair families directly: "define and implement a trait or new type instead". When the implementing type is a primitive, the header's wording changes to "implemented for primitive types" and the label names the primitive, as the Drop trap at the end shows; the structure is otherwise the same. The reading above is scoped to the forms printed in this guide.
The first failure: Display for Vec
The canonical shape - the Book's own example of what the orphan rule forbids, an impl of Display for Vec attempted from a crate that defines neither:
use std::fmt;
impl fmt::Display for Vec<i32> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.len())
}
}
fn main() {
println!("{}", vec![1, 2, 3]);
}
$ rustc --edition 2021 fail_display_vec.rs -o fail_display_vec
error[E0117]: only traits defined in the current crate can be implemented for types defined outside of the crate
--> fail_display_vec.rs:2:1
|
2 | impl fmt::Display for Vec<i32> {
| ^^^^^^^^^^^^^^^^^^^^^^--------
| |
| `Vec` is not defined in the current crate
|
= note: impl doesn't have any local type before any uncovered type parameters
= note: for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules
= note: define and implement a trait or new type instead
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0117`.
use std::fmt;
struct IntList(Vec<i32>);
impl fmt::Display for IntList {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "list of {}", self.0.len())
}
}
fn main() {
println!("{}", IntList(vec![1, 2, 3]));
}
$ rustc --edition 2021 fix_display_vec.rs -o fix_display_vec
$ ./fix_display_vec
list of 3
Display is defined in the standard library and Vec is defined in the standard library; the impl's crate defines neither, and Display takes no type parameters that could supply a local type. There is nothing local anywhere in the impl, so there is no crate positioned to own it. The fix defines a local type that wraps the vector - IntList - and implements Display for the wrapper. The implementing type is now local, the rule is satisfied, and the body of fmt reads the vector through the wrapper's field.
The output is list of 3. The wrapper is the general repair for this error: it costs one struct definition and a .0 at each use, and in exchange the impl - and every method and trait the wrapper later needs - has a local home. Where the wrapper is not acceptable, the alternatives further down are a trait of your own, or a local type in the trait's parameters.
The second failure: Display for a fixed-size array
An array type is no more local than a vector:
use std::fmt;
impl fmt::Display for [i32; 3] {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self[0])
}
}
fn main() {
println!("{}", [1, 2, 3]);
}
$ rustc --edition 2021 fail_display_array.rs -o fail_display_array
error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
--> fail_display_array.rs:2:1
|
2 | impl fmt::Display for [i32; 3] {
| ^^^^^^^^^^^^^^^^^^^^^^--------
| |
| this is not defined in the current crate because arrays are always foreign
|
= note: impl doesn't have any local type before any uncovered type parameters
= note: for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules
= note: define and implement a trait or new type instead
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0117`.
use std::fmt;
struct Scores([i32; 3]);
impl fmt::Display for Scores {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "scores {} {} {}", self.0[0], self.0[1], self.0[2])
}
}
fn main() {
println!("{}", Scores([1, 2, 3]));
}
$ rustc --edition 2021 fix_display_array.rs -o fix_display_array
$ ./fix_display_array
scores 1 2 3
[i32; 3] is a built-in type, defined by the language rather than by this crate, and the label under the span says so in its own form: "this is not defined in the current crate because arrays are always foreign". The size and the element type are both part of the type, and neither is local. Nothing about the array being small, fixed-size, or stack-allocated changes which crate defines it.
The fix is the same wrapper in a different shape: Scores holds the array as its one field, Display is implemented for Scores, and the output is scores 1 2 3 - the three elements read through the wrapper. A wrapper around a built-in type is still a local type; the rule cares about the outermost type in the impl, and here that type is Scores.
The third failure: Display for a tuple
A tuple type is built in too, whatever it holds:
use std::fmt;
impl fmt::Display for (i32, i32) {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
fn main() {
println!("{}", (1, 2));
}
$ rustc --edition 2021 fail_display_tuple.rs -o fail_display_tuple
error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
--> fail_display_tuple.rs:2:1
|
2 | impl fmt::Display for (i32, i32) {
| ^^^^^^^^^^^^^^^^^^^^^^----------
| |
| this is not defined in the current crate because tuples are always foreign
|
= note: impl doesn't have any local type before any uncovered type parameters
= note: for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules
= note: define and implement a trait or new type instead
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0117`.
use std::fmt;
struct Point { x: i32, y: i32 }
impl fmt::Display for Point {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "({}, {})", self.x, self.y)
}
}
fn main() {
println!("{}", Point { x: 1, y: 2 });
}
$ rustc --edition 2021 fix_display_tuple.rs -o fix_display_tuple
$ ./fix_display_tuple
(1, 2)
(i32, i32) is a tuple type, defined by the language. A tuple's elements do not give it a local identity: the type as a whole is foreign, and the impl has no local type in it. Tuples are the shape where this is easiest to miss, because a pair of values feels anonymous enough to be unowned - the rule treats it as owned by the language, like any other built-in type.
The fix replaces the tuple with a local struct that has the same two fields. Point is defined in this crate, so Display can be implemented for it directly, with no wrapper field to read through: the output is (1, 2), printed from the struct's own fields. When the tuple was only ever a pair of named quantities, a struct is not a workaround but the plainer definition.
The fourth failure: From> for String
Every position in the impl is foreign at once - trait, implementing type, and the trait's parameter:
impl From<Vec<i32>> for String {
fn from(v: Vec<i32>) -> String {
format!("{} items", v.len())
}
}
fn main() {
let s = String::from(vec![1, 2]);
println!("{}", s);
}
$ rustc --edition 2021 fail_from_vec_string.rs -o fail_from_vec_string
error[E0117]: only traits defined in the current crate can be implemented for types defined outside of the crate
--> fail_from_vec_string.rs:1:1
|
1 | impl From<Vec<i32>> for String {
| ^^^^^--------------^^^^^------
| | |
| | `String` is not defined in the current crate
| `Vec` is not defined in the current crate
|
= note: impl doesn't have any local type before any uncovered type parameters
= note: for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules
= note: define and implement a trait or new type instead
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0117`.
struct IntList(Vec<i32>);
impl From<IntList> for String {
fn from(v: IntList) -> String {
format!("{} items", v.0.len())
}
}
fn main() {
let s = String::from(IntList(vec![1, 2]));
println!("{}", s);
}
$ rustc --edition 2021 fix_from_vec_string.rs -o fix_from_vec_string
$ ./fix_from_vec_string
2 items
From is a standard-library trait, String is a standard-library type, and the parameter Vec<i32> is foreign as well. The index's statement of the rule covers exactly this stacking: the error applies where the implementing type is foreign and all of the parameters being passed to the trait are also foreign. Any one local type, in any of the three positions, would have satisfied the rule; this impl has none.
The fix supplies the local type in the parameter position. IntList is the first family's wrapper, and From<IntList> for String is a legal impl even though String is still foreign, because the parameter is local - the same construction the index uses in its own repair example, impl From<Foo> for i32, where a local type as the parameter carries the impl. The output is 2 items, the string the conversion builds.
The fifth failure: Display for Vec over a generic parameter
Making the impl generic does not make it local:
use std::fmt;
impl<T: fmt::Display> fmt::Display for Vec<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.len())
}
}
fn main() {
println!("{}", vec![1, 2]);
}
$ rustc --edition 2021 fail_display_vec_generic.rs -o fail_display_vec_generic
error[E0117]: only traits defined in the current crate can be implemented for types defined outside of the crate
--> fail_display_vec_generic.rs:2:1
|
2 | impl<T: fmt::Display> fmt::Display for Vec<T> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^------
| |
| `Vec` is not defined in the current crate
|
= note: impl doesn't have any local type before any uncovered type parameters
= note: for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules
= note: define and implement a trait or new type instead
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0117`.
use std::fmt;
struct List<T>(Vec<T>);
impl<T: fmt::Display> fmt::Display for List<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "list of {}", self.0.len())
}
}
fn main() {
println!("{}", List(vec![1, 2]));
}
$ rustc --edition 2021 fix_display_vec_generic.rs -o fix_display_vec_generic
$ ./fix_display_vec_generic
list of 2
The impl is written for every Vec<T> at once, and a type parameter is not a type defined in this crate - it stands for types defined anywhere, including types that do not exist yet. An impl that claimed Display for Vec<T> for all T would claim it for vectors of types owned by other crates too, which is precisely the unowned claim the rule refuses. The bound T: Display constrains which types the impl applies to; it does not localize any of them.
The fix wraps generically: List<T> is a local generic type holding a Vec<T>, and its Display impl is as general as the failed one - the same bound, the same body shape - but the implementing type is local. The output is list of 2. A generic wrapper keeps the generality the original impl wanted; what it cannot keep is the foreign type at the head of the impl.
The sixth failure: the impl moved inside a module
Nesting the impl changes its scope, not its crate:
mod m {
use std::fmt;
impl fmt::Display for Vec<i32> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.len())
}
}
}
fn main() {
println!("{}", vec![1, 2, 3].len());
}
$ rustc --edition 2021 fail_nested_module.rs -o fail_nested_module
error[E0117]: only traits defined in the current crate can be implemented for types defined outside of the crate
--> fail_nested_module.rs:3:5
|
3 | impl fmt::Display for Vec<i32> {
| ^^^^^^^^^^^^^^^^^^^^^^--------
| |
| `Vec` is not defined in the current crate
|
= note: impl doesn't have any local type before any uncovered type parameters
= note: for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules
= note: define and implement a trait or new type instead
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0117`.
use std::fmt;
struct IntList(Vec<i32>);
impl fmt::Display for IntList {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "list of {}", self.0.len())
}
}
fn main() {
println!("{}", IntList(vec![1, 2, 3]));
}
$ rustc --edition 2021 fix_nested_module.rs -o fix_nested_module
$ ./fix_nested_module
list of 3
The impl sits inside mod m, and the error is identical to the first family's, down to the label. The orphan rule is a per-crate rule: the index and the Reference both state it in terms of the current crate, and a module is a namespace inside a crate, not a smaller crate. The same is true of an impl written inside a function body - it is in the extras below, with the same single E0117. No arrangement of the code inside the crate gives the impl a different owner.
The fix is therefore not a rearrangement. It is the first family's wrapper again, written at the crate root where the impl's users are: IntList is defined by the crate, Display is implemented for it, and the output is list of 3. Moving a failing impl is never the repair; giving it a local type is.
The seventh failure: Display for Box
A fundamental type with a foreign type inside it:
use std::fmt;
impl fmt::Display for Box<i32> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "boxed {}", **self)
}
}
fn main() {
println!("{}", Box::new(7));
}
$ rustc --edition 2021 fail_display_box_foreign.rs -o fail_display_box_foreign
error[E0117]: only traits defined in the current crate can be implemented for types defined outside of the crate
--> fail_display_box_foreign.rs:2:1
|
2 | impl fmt::Display for Box<i32> {
| ^^^^^^^^^^^^^^^^^^^^^^--------
| |
| `i32` is not defined in the current crate
|
= note: impl doesn't have any local type before any uncovered type parameters
= note: for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules
= note: define and implement a trait or new type instead
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0117`.
use std::fmt;
struct Local(i32);
impl fmt::Display for Box<Local> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "boxed {}", self.0)
}
}
fn main() {
println!("{}", Box::new(Local(7)));
}
$ rustc --edition 2021 fix_display_box_foreign.rs -o fix_display_box_foreign
$ ./fix_display_box_foreign
boxed 7
Box is one of the Reference's fundamental types - transparent for coherence - which cuts both ways. It means the box itself neither supplies locality nor blocks it: what decides the impl is the type inside the box. Here that type is i32, a foreign primitive, and it is uncovered in the Reference's sense - the T in Box<T> is not considered covered - so the impl has no local type and fails, with the label naming i32 as the type not defined in the current crate.
The fix changes only the type inside the box. Box<Local>, where Local is a struct defined in this crate, is considered a local type by the same fundamental-type rule, so Display can be implemented for it directly - no wrapper struct around the box is needed. The output is boxed 7, read from the local value inside the box. This is the one family whose repair keeps the foreign head type in the impl, and it works only because Box is fundamental; the next family shows the ordinary generic type where it does not.
The eighth failure: Display for Vec of a local type
A local element type inside a foreign generic type:
use std::fmt;
struct Local(i32);
impl fmt::Display for Vec<Local> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "vec of {}", self.len())
}
}
fn main() {
println!("{}", vec![Local(1), Local(2)]);
}
$ rustc --edition 2021 fail_display_vec_local.rs -o fail_display_vec_local
error[E0117]: only traits defined in the current crate can be implemented for types defined outside of the crate
--> fail_display_vec_local.rs:3:1
|
3 | impl fmt::Display for Vec<Local> {
| ^^^^^^^^^^^^^^^^^^^^^^----------
| |
| `Vec` is not defined in the current crate
|
= note: impl doesn't have any local type before any uncovered type parameters
= note: for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules
= note: define and implement a trait or new type instead
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0117`.
use std::fmt;
struct Local(i32);
struct LocalList(Vec<Local>);
impl fmt::Display for LocalList {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let total: i32 = self.0.iter().map(|l| l.0).sum();
write!(f, "locals sum {}", total)
}
}
fn main() {
println!("{}", LocalList(vec![Local(1), Local(2)]));
}
$ rustc --edition 2021 fix_display_vec_local.rs -o fix_display_vec_local
$ ./fix_display_vec_local
locals sum 3
This is the shape that looks most like it should pass. Local is defined in this crate, it appears in the implementing type, and the impl is only about vectors of this crate's own type. It still fails, with the same label as the first family: "Vec is not defined in the current crate". Vec is not a fundamental type, so the locality scan looks at the outermost type, finds Vec, and stops - a local type nested inside a non-fundamental foreign type does not make the whole type local. Coverage and locality are different properties: the Local here is covered, in that it sits inside another type, but covered is not local.
The fix gives the collection a local head: LocalList wraps the Vec<Local>, and Display is implemented for the wrapper. The output is locals sum 3 - the wrapper's fmt iterates the vector and sums the local values' fields, which the wrapper is free to do because both types are now within its reach. Compare the seventh family: Box<Local> compiles and Vec<Local> does not, and the entire difference is that Box is fundamental and Vec is not.
The ninth failure: Sum for Vec
A foreign trait with a parameter, where the parameter is foreign too:
impl std::iter::Sum<i32> for Vec<i32> {
fn sum<I: Iterator<Item = i32>>(iter: I) -> Self {
iter.collect()
}
}
fn main() {
let v: Vec<i32> = vec![1, 2, 3];
println!("{}", v.len());
}
$ rustc --edition 2021 fail_sum_vec.rs -o fail_sum_vec
error[E0117]: only traits defined in the current crate can be implemented for types defined outside of the crate
--> fail_sum_vec.rs:1:1
|
1 | impl std::iter::Sum<i32> for Vec<i32> {
| ^^^^^-------------------^^^^^--------
| | |
| | `Vec` is not defined in the current crate
| `i32` is not defined in the current crate
|
= note: impl doesn't have any local type before any uncovered type parameters
= note: for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules
= note: define and implement a trait or new type instead
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0117`.
struct IntList(Vec<i32>);
impl std::iter::Sum<i32> for IntList {
fn sum<I: Iterator<Item = i32>>(iter: I) -> Self {
IntList(iter.collect())
}
}
fn main() {
let total: IntList = vec![1, 2, 3].into_iter().sum();
println!("{}", total.0.len());
}
$ rustc --edition 2021 fix_sum_vec.rs -o fix_sum_vec
$ ./fix_sum_vec
3
Sum is a standard-library trait with a type parameter - the element type being summed - and here both the parameter i32 and the implementing type Vec<i32> are foreign. A trait parameter does not by itself give an impl a local foothold; the fourth family showed a local parameter carrying an impl, and this is the mirror image, where the parameter is as foreign as everything else. The diagnostic is the same single E0117, on the impl header.
The fix is the wrapper again, in its trait-implementing form: IntList implements Sum<i32>, collecting the iterator into its own vector field. The output is 3, the length of the collected vector read back in main. The wrapper does not just satisfy the rule formally - it is also the type a caller names to ask for the sum, which is what makes the impl discoverable.
The tenth failure: Display for a slice
An unsized built-in type:
use std::fmt;
impl fmt::Display for [i32] {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.len())
}
}
fn main() {
let v = vec![1, 2, 3];
println!("{}", &v[..]);
}
$ rustc --edition 2021 fail_display_slice.rs -o fail_display_slice
error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
--> fail_display_slice.rs:2:1
|
2 | impl fmt::Display for [i32] {
| ^^^^^^^^^^^^^^^^^^^^^^-----
| |
| this is not defined in the current crate because slices are always foreign
|
= note: impl doesn't have any local type before any uncovered type parameters
= note: for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules
= note: define and implement a trait or new type instead
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0117`.
use std::fmt;
struct IntSlice<'a>(&'a [i32]);
impl fmt::Display for IntSlice<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "slice of {}", self.0.len())
}
}
fn main() {
let v = vec![1, 2, 3];
println!("{}", IntSlice(&v));
}
$ rustc --edition 2021 fix_display_slice.rs -o fix_display_slice
$ ./fix_display_slice
slice of 3
[i32] - the slice type, without a length - is a built-in type like the array of the second family, and foreign for the same reason. It is also unsized. A struct can declare a slice as its last field, but the struct is then unsized itself and cannot be constructed or passed by value the way the other families' wrappers are, so the usable wrapper holds a reference to the slice instead.
The fix does exactly that. IntSlice<'a> is a local struct whose field is &'a [i32]; it is sized, it is local, and Display is implemented for it. The output is slice of 3. The lifetime parameter is part of the wrapper, not a complication the rule adds - a type parameter of any kind on a local type leaves the type local, as the fifth family's fix and the generic shapes below also show.
The eleventh failure: Display for HashMap
A standard-library generic type with every parameter foreign:
use std::fmt;
use std::collections::HashMap;
impl fmt::Display for HashMap<String, i32> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.len())
}
}
fn main() {
let mut m: HashMap<String, i32> = HashMap::new();
m.insert(String::from("a"), 1);
println!("{}", m);
}
$ rustc --edition 2021 fail_display_hashmap.rs -o fail_display_hashmap
error[E0117]: only traits defined in the current crate can be implemented for types defined outside of the crate
--> fail_display_hashmap.rs:3:1
|
3 | impl fmt::Display for HashMap<String, i32> {
| ^^^^^^^^^^^^^^^^^^^^^^--------------------
| |
| `HashMap` is not defined in the current crate
|
= note: impl doesn't have any local type before any uncovered type parameters
= note: for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules
= note: define and implement a trait or new type instead
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0117`.
use std::fmt;
use std::collections::HashMap;
struct Counts(HashMap<String, i32>);
impl fmt::Display for Counts {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "map of {}", self.0.len())
}
}
fn main() {
let mut m: HashMap<String, i32> = HashMap::new();
m.insert(String::from("a"), 1);
println!("{}", Counts(m));
}
$ rustc --edition 2021 fix_display_hashmap.rs -o fix_display_hashmap
$ ./fix_display_hashmap
map of 1
HashMap is a standard-library type, and both of its parameters here - String for the keys, i32 for the values - are foreign. This is the eighth family's lesson at one more remove: not only is the head type foreign, there is no local type anywhere in the impl for the scan to find, in the parameters or anywhere else. Named standard types with several parameters fail exactly as Vec does; the number of parameters is irrelevant when none of them is local and the head is not fundamental.
The fix is the wrapper once more: Counts holds the map, implements Display, and prints map of 1 - the entry count read through the wrapper. Eleven families in, the wrapper has been the repair in most of them, and that is the rule's intent: the diagnostic's own closing note offers "a trait or new type", and the new type is the option that fits an impl whose trait and purpose are both worth keeping.
More shapes that are E0117
The same single error, in forms that follow the families above. Each program below fails with exactly one E0117 and no other error:
DisplayforString- a named standard type with no parameters at all, foreign on its own:
use std::fmt;
impl fmt::Display for String {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "x") }
}
fn main() {
println!("s");
}
Cloneforbool- a derivable standard trait for a primitive; that the impl body is trivial does not matter:
impl Clone for bool {
fn clone(&self) -> bool { *self }
}
fn main() {
println!("{}", true);
}
Defaultforu64- the same shape with the value the impl would return differing from the standard one; the rule is checked before any question of which default is better:
impl Default for u64 {
fn default() -> u64 { 7 }
}
fn main() {
println!("{}", 1u64);
}
Displayfor a function-pointer type - a built-in type like the tuple and the array:
use std::fmt;
impl fmt::Display for fn(i32) -> i32 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "x") }
}
fn main() {
println!("f");
}
Displayfor&Vec<i32>- a reference is fundamental, likeBox, so it is transparent: the type inside isVec<i32>, which is foreign, and the impl fails as the seventh family's does:
use std::fmt;
impl fmt::Display for &Vec<i32> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "x") }
}
fn main() {
let v = vec![1];
println!("{}", v.len());
}
DisplayforOption<i32>-Optionis an ordinary generic type, not a fundamental one, so this fails the wayVec<i32>does:
use std::fmt;
impl fmt::Display for Option<i32> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "x") }
}
fn main() {
println!("{}", Some(1).is_some());
}
- The first family's impl written inside
main- position inside the crate is irrelevant, as the sixth family showed for modules:
use std::fmt;
fn main() {
impl fmt::Display for Vec<i32> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "x") }
}
println!("{}", vec![1].len());
}
DebugforVec<i32>- the standard library already implementsDebugfor this type, and the error is still E0117, not a conflicting-impl error: the orphan check is the one this impl fails, and it is reported first:
use std::fmt;
impl fmt::Debug for Vec<i32> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "v") }
}
fn main() {
println!("{:?}", vec![1]);
}
The alternative repairs: a local trait, and a local type as the trait's parameter
The wrapper is not the only repair. Where the foreign trait itself is not essential - the caller just needs the behavior - a trait defined in this crate can be implemented for the foreign type directly, because a local trait satisfies the rule on its own:
trait Show {
fn show(&self) -> String;
}
impl Show for Vec<i32> {
fn show(&self) -> String {
format!("vec of {}", self.len())
}
}
fn main() {
println!("{}", vec![1, 2, 3].show());
}
$ rustc --edition 2021 alt_local_trait.rs -o alt_local_trait
$ ./alt_local_trait
vec of 3
struct Local(i32);
impl From<Local> for Vec<i32> {
fn from(v: Local) -> Vec<i32> {
vec![v.0]
}
}
fn main() {
let v: Vec<i32> = Local(5).into();
println!("{}", v.len());
}
$ rustc --edition 2021 alt_from_local_param_vec.rs -o alt_from_local_param_vec
$ ./alt_from_local_param_vec
1
The first program defines Show locally and implements it for Vec<i32>: the output is vec of 3, and no wrapper appears anywhere. This is the index's own second repair - "define a trait locally and implement that instead" - and its cost is that callers must use the new trait: code written against Display, including println! formatting, cannot see it. The second program is the parameter repair in the opposite arrangement from the fourth family's fix: From<Local> for Vec<i32> implements a foreign trait for a foreign type, carried entirely by the local type in the parameter position, and the output is 1, the length of the converted vector. Between the wrapper, the local trait, and the local parameter, every family in this guide has at least one repair that keeps its behavior.
Shapes that compile as written
- A local trait implemented for a foreign type. The trait being local satisfies the rule by itself - this is the alternative repair above standing alone, in the index's own form:
trait Bar {
fn get(&self) -> usize;
}
impl Bar for u32 {
fn get(&self) -> usize { 0 }
}
fn main() {
println!("{}", 5u32.get());
}
$ rustc --edition 2021 ok_local_trait_foreign.rs -o ok_local_trait_foreign
$ ./ok_local_trait_foreign
0
use std::fmt;
struct Foo { x: i32 }
impl fmt::Display for Foo {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "foo {}", self.x)
}
}
fn main() {
println!("{}", Foo { x: 1 });
}
$ rustc --edition 2021 ok_foreign_trait_local.rs -o ok_foreign_trait_local
$ ./ok_foreign_trait_local
foo 1
The first program prints 0, the value its Bar impl returns for a u32. The second is the mirror image: a foreign trait, Display, implemented for a local type, Foo, printing foo 1. Either side being local is enough; every family above is a case where neither side was.
- A local trait for a local type, and a foreign trait whose parameter is local. The first is the unremarkable center of the rule - both sides local. The second is the index's repair example:
From<Foo> for i32, a foreign trait and a foreign implementing type, carried by the local type in the parameter:
struct Foo { x: i32 }
trait Show {
fn show(&self) -> i32;
}
impl Show for Foo {
fn show(&self) -> i32 { self.x }
}
fn main() {
println!("{}", Foo { x: 3 }.show());
}
$ rustc --edition 2021 ok_local_trait_local.rs -o ok_local_trait_local
$ ./ok_local_trait_local
3
pub struct Foo { x: i32 }
impl From<Foo> for i32 {
fn from(i: Foo) -> i32 { i.x }
}
fn main() {
let n: i32 = Foo { x: 4 }.into();
println!("{}", n);
}
$ rustc --edition 2021 ok_from_local_param.rs -o ok_from_local_param
$ ./ok_from_local_param
4
The outputs are 3 and 4 - the local value read through the local trait, and the integer the conversion produces.
- A foreign trait for a reference to a local type, and for a foreign type with a local parameter. A reference is fundamental, so
&Localcounts as a local type andDisplaycan be implemented for it.Add<Num> for i32is the parameter repair with an operator trait: the implementing type is a primitive, and the localNumin the parameter position carries the impl:
use std::fmt;
struct Local(i32);
impl fmt::Display for &Local {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "ref {}", self.0)
}
}
fn main() {
println!("{}", &Local(7));
}
$ rustc --edition 2021 ok_ref_local.rs -o ok_ref_local
$ ./ok_ref_local
ref 7
struct Num(i32);
impl std::ops::Add<Num> for i32 {
type Output = i32;
fn add(self, rhs: Num) -> i32 { self + rhs.0 }
}
fn main() {
println!("{}", 1 + Num(2));
}
$ rustc --edition 2021 ok_add_local_param.rs -o ok_add_local_param
$ ./ok_add_local_param
3
The outputs are ref 7 and 3. Together with the seventh family's Box<Local>, the reference case completes the fundamental-type picture: the transparent outer type defers to the local type inside it.
- A blanket impl of a local trait, and a foreign trait for a generic local type. A local trait may be implemented for every
Tat once, because the trait's locality is what the rule asks for - contrast the blanket trap below, where the trait is foreign. And a local type stays local when it is generic:Pair<T>is defined in this crate, so itsDisplayimpl is an ordinary local impl, with the parameter appearing in the local type's own field:
use std::fmt;
trait Label {
fn label(&self) -> String;
}
impl<T: fmt::Display> Label for T {
fn label(&self) -> String { format!("item {}", self) }
}
fn main() {
println!("{}", 7.label());
}
$ rustc --edition 2021 ok_blanket_local_trait.rs -o ok_blanket_local_trait
$ ./ok_blanket_local_trait
item 7
use std::fmt;
struct Pair<T> { first: T }
impl<T: fmt::Display> fmt::Display for Pair<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "pair {}", self.first)
}
}
fn main() {
println!("{}", Pair { first: 9 });
}
$ rustc --edition 2021 ok_generic_local_self.rs -o ok_generic_local_self
$ ./ok_generic_local_self
pair 9
The outputs are item 7 and pair 9.
Neighbors that are not E0117
- A second impl of the same trait for the same local type is E0119, conflicting implementations. Locality is satisfied - the type is local - and the impl still fails, because coherence has a second half: two impls that could apply to the same type are one too many. This is the error the orphan rule's defenders point to: it is what would become undecidable across crates if orphan impls were allowed:
use std::fmt;
struct Foo { x: i32 }
impl fmt::Display for Foo {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "a") }
}
impl fmt::Display for Foo {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "b") }
}
fn main() {
println!("{}", Foo { x: 1 });
}
$ rustc --edition 2021 trap_conflicting.rs -o trap_conflicting
error[E0119]: conflicting implementations of trait `std::fmt::Display` for type `Foo`
--> trap_conflicting.rs:6:1
|
3 | impl fmt::Display for Foo {
| ------------------------- first implementation here
...
6 | impl fmt::Display for Foo {
| ^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `Foo`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0119`.
use std::fmt;
struct Foo { x: i32 }
impl fmt::Display for Foo {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "a") }
fn extra(&self) -> i32 { self.x }
}
fn main() {
println!("{}", Foo { x: 1 });
}
$ rustc --edition 2021 trap_method_not_in_trait.rs -o trap_method_not_in_trait
error[E0407]: method `extra` is not a member of trait `fmt::Display`
--> trap_method_not_in_trait.rs:5:5
|
5 | fn extra(&self) -> i32 { self.x }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not a member of trait `fmt::Display`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0407`.
The repair is to delete one of the two impls, or to differentiate them so they cannot both apply. The second program is an impl that adds a method the trait does not define: extra is not part of Display, and a trait impl may only implement the trait's own items, so the error is E0407, a method not a member of the trait. The repair is to move the method into an inherent impl on the local type, or to add it to a local trait.
- A required method left out of the impl is E0046, a missing trait item. The impl's locality is fine; its contents are incomplete:
use std::fmt;
struct Foo { x: i32 }
impl fmt::Display for Foo {}
fn main() {
println!("{}", Foo { x: 1 });
}
$ rustc --edition 2021 trap_missing_method.rs -o trap_missing_method
error[E0046]: not all trait items implemented, missing: `fmt`
--> trap_missing_method.rs:3:1
|
3 | impl fmt::Display for Foo {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^ missing `fmt` in implementation
|
= help: implement the missing item: `fn fmt(&self, _: &mut Formatter<'_>) -> Result<(), std::fmt::Error> { todo!() }`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0046`.
use std::fmt;
struct Local(i32);
impl<T> fmt::Display for T {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "x") }
}
fn main() {
println!("{}", Local(1).0);
}
$ rustc --edition 2021 trap_uncovered_blanket.rs -o trap_uncovered_blanket
error[E0210]: type parameter `T` must be used as an argument to some local type (e.g., `MyStruct<T>`)
--> trap_uncovered_blanket.rs:3:6
|
3 | impl<T> fmt::Display for T {
| ^ uncovered type parameter
|
= note: implementing a foreign trait is only possible if at least one of the types for which it is implemented is local
= note: only traits defined in the current crate can be implemented for a type parameter
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0210`.
The repair is to implement the missing item - Display requires fmt. The second program is the blanket form of the orphan problem: impl<T> Display for T tries to implement a foreign trait for a bare type parameter, and the error is E0210, not E0117 - a type parameter must be covered by another type when it appears before the first local type in the impl. There is no local type anywhere in that impl for the parameter to precede, and the compiler gives the uncovered-parameter defect its own code. Compare the compiling blanket impl above: the same shape with a local trait is legal, because the local-trait branch of the rule never reaches the parameter scan.
- An inherent impl on a foreign type is E0116. Inherent impls - the ones with no trait - have a stricter rule than trait impls: the type must be defined in the current crate outright, with no local-trait or local-parameter escape, because there is no trait in the impl to be local:
impl String {
fn shout(&self) -> String { self.to_uppercase() }
}
fn main() {
println!("a");
}
$ rustc --edition 2021 trap_inherent_foreign.rs -o trap_inherent_foreign
error[E0116]: cannot define inherent `impl` for a type outside of the crate where the type is defined
--> trap_inherent_foreign.rs:1:1
|
1 | impl String {
| ^^^^^^^^^^^ impl for type defined outside of crate
|
= help: consider defining a trait and implementing it for the type or using a newtype wrapper like `struct MyType(ExternalType);` and implement it
= note: for more details about the orphan rules, see <https://doc.rust-lang.org/reference/items/implementations.html?highlight=orphan#orphan-rules>
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0116`.
use std::fmt;
struct Foo { x: i32 }
impl fmt::Display for Foo {
fn fmt(&self) -> fmt::Result { Ok(()) }
}
fn main() {
println!("{}", Foo { x: 1 });
}
$ rustc --edition 2021 trap_signature_mismatch.rs -o trap_signature_mismatch
error[E0050]: method `fmt` has 1 parameter but the declaration in trait `std::fmt::Display::fmt` has 2
--> trap_signature_mismatch.rs:4:12
|
4 | fn fmt(&self) -> fmt::Result { Ok(()) }
| ^^^^^ expected 2 parameters, found 1
|
= note: `fmt` from trait: `fn(&Self, &mut Formatter<'_>) -> Result<(), std::fmt::Error>`
help: add the missing parameter from the trait
|
4 | fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { Ok(()) }
| +++++++++++++++++++++++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0050`.
The repair is a local trait carrying the method - the alternative repair above - or a wrapper. The second program implements the right trait for the right local type with the wrong method shape: fmt without its formatter parameter is E0050, a method with an incompatible signature for the trait. Locality never gets a say; the impl fails on its contents.
Dropfor a primitive reports E0117 together with a second error, E0120.Dropis the index's own example of an orphan impl, and it is the one common trait whose impls are restricted further: theDroptrait may only be implemented for local structs, enums, and unions, so the primitive impl fails both checks in one compile:
impl Drop for u32 {
fn drop(&mut self) {}
}
fn main() {
println!("{}", 1u32);
}
$ rustc --edition 2021 trap_drop_primitive.rs -o trap_drop_primitive
error[E0117]: only traits defined in the current crate can be implemented for primitive types
--> trap_drop_primitive.rs:1:1
|
1 | impl Drop for u32 {
| ^^^^^^^^^^^^^^---
| |
| `u32` is not defined in the current crate
|
= note: impl doesn't have any local type before any uncovered type parameters
= note: for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules
= note: define and implement a trait or new type instead
error[E0120]: the `Drop` trait may only be implemented for local structs, enums, and unions
--> trap_drop_primitive.rs:1:15
|
1 | impl Drop for u32 {
| ^^^ must be a struct, enum, or union in the current crate
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0117, E0120.
For more information about an error, try `rustc --explain E0117`.
Note the header's variant wording - "implemented for primitive types" - and the closing line naming both codes. The repair is the general one: implement Drop for a local type that owns the value, which is also the only form in which a Drop impl is ever useful.
Checklist
- The label checked: E0117's header states the rule in general terms, and the label under the span names the type that is not local - if the code is E0119, E0210, E0116, E0407, E0046, E0050, or E0120, the neighbors name the difference before any edit.
- Both sides of the impl inventoried: the trait, the implementing type, and every trait parameter - the impl is legal if any one of them is local, in the right position, and illegal only when none of them is.
- Fundamental types checked before wrapping: for
Box, a reference, or another fundamental type, the type inside decides - a local type inside may make a wrapper unnecessary. - Non-fundamental generics not assumed local:
Vec<Local>andOption<Local>are foreign types; a local type inside them does not carry the impl, and a wrapper is required. - Type parameters read for coverage, not locality: a covered parameter inside a foreign type is fine when a local type follows it in the scan, and a bare parameter as the implementing type is E0210, a different error with a different repair.
- Position inside the crate ignored: moving the impl between modules, or into or out of a function, never changes the verdict - the crate is the unit.
- The repair matched to the need: a wrapper when the foreign trait must be satisfied as-is, a local trait when only the behavior matters, a local parameter when the impl's shape has a parameter position to put one in.
- The fixed program run, and its output checked against what the program was meant to print - an impl moved onto a wrapper changes the type callers name, and every use site has to name the wrapper too.
Related guides on this site
- E0210 Type Parameter Orphan Rule Fix
- E0091 Unused Type Parameter Fix
- E0084 Unsupported Representation Fix
- E0130 Patterns in Foreign Function Fix
Sources
- Rust, Error codes index: E0117 — for the definition of the error as only traits defined in the current crate being implementable for arbitrary types, for the
Dropforu32example, for the rule as a foreign trait with a foreign implementing type and all-foreign parameters, and for the two repairs: a local type referenced by the impl, as inFrom<Foo> for i32, or a locally defined trait implemented instead. - The Rust Reference, Implementations: orphan rules — for the orphan rule as allowing an impl only when the trait or at least one type in it is defined in the current crate, for the formal scan over the implementing type and the trait's parameters with no uncovered type parameter before the first local type, and for fundamental types, where the parameter in
Box<T>is not covered andBox<LocalType>is considered local. - The Rust Programming Language, Traits: Defining Shared Behavior — for coherence and the orphan rule as the restriction that keeps two crates from implementing the same trait for the same type, and for the example of
Displaynot being implementable forVec<T>from a crate that defines neither.
Keep Exploring
The full Rust hub \u2014 ownership, structs and enums, async, and measured comparisons.
Every guide on the site in one list, grouped by topic.
Every browser-only checker and verifier on the site.
The C++, Python, Sysadmin, Rust, Go, and TypeScript hubs.