Email me

Rust errors / Types and traits

Rust compiler error · checked with rustc 1.99.0

Rust error E0117: only traits defined in the current crate can be implemented for types defined outside of the crate

What it meansThe orphan rule: you can implement a trait for a type only if your crate defines the trait or the type. Implementing a foreign trait (like Display) for a foreign type (like Vec or another crate's struct) is not allowed.

What rustc prints

error[E0117]: only traits defined in the current crate can be implemented for primitive types
 --> main.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

Real output of rustc 1.99.0 compiling the official example below (2021 edition). Your names and line numbers will differ; the first line is the part to match.

Why it happens

  • impl Display for Vec<MyThing>.
  • impl From<reqwest::Error> for std::io::Error.
  • Implementing serde traits for a type from another crate.

How to fix it

1. Wrap it in a newtype

Your wrapper is a local type, so the impl is allowed.

struct Names(Vec<String>);
impl std::fmt::Display for Names {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self.0.join(", "))
    }
}

2. Use your own trait

A local trait can be implemented for any type.

trait Pretty { fn pretty(&self) -> String; }
impl Pretty for Vec<String> { fn pretty(&self) -> String { self.join(", ") } }

3. For serde, use remote derive or with=

serde supports deriving for types from other crates.

#[serde(with = "time::serde::rfc3339")]
created: time::OffsetDateTime,

Other wordings of this error

only traits defined in the current crate can be implemented for types defined outside of the crate, only traits defined in the current crate can be implemented for primitive types, impl doesn't use only types from inside the current crate, orphan rule

E0117Types and traits
Activein rustc 1.99.0
8related errors linked below

The compiler team's explanation

Only traits defined in the current crate can be implemented for arbitrary types.

Erroneous code example:

impl Drop for u32 {}

This error indicates a violation of one of Rust's orphan rules for trait implementations. The rule prohibits any implementation of a foreign trait (a trait defined in another crate) where

  • the type that is implementing the trait is foreign
  • all of the parameters being passed to the trait (if there are any) are also foreign.

To avoid this kind of error, ensure that at least one local type is referenced by the impl:

pub struct Foo; // you define your type in your crate

impl Drop for Foo { // and you can implement the trait on it!
    // code of trait implementation here
}

impl From<Foo> for i32 { // or you use a type from your crate as
                         // a type parameter
    fn from(i: Foo) -> i32 {
        0
    }
}

Alternatively, define a trait locally and implement that instead:

trait Bar {
    fn get(&self) -> usize;
}

impl Bar for u32 {
    fn get(&self) -> usize { 0 }
}

For information on the design of the orphan rules, see RFC 1023.

From rustc --explain E0117 (1.99.0), part of the Rust project, MIT or Apache-2.0. Official page.

Related errors

All 518 Rust error codes · Rust crate data · Glossary

Up next

Often hit nextE0308: mismatched typesA related error from the same family.Read it next →

Did this answer your question?

Get the receipts by email.

What my AI agents tried this week, with the real numbers: what worked, what flopped, what made money. One email when there is something worth reading.

No spam, never sold. Every email has a one-click unsubscribe.

Popular in the shop

All 124 tools →

Browse: Bundles and kitsHome, family and schoolMoney and businessAI toolsSports and fitnessMaking and hobbiesFood and drinkIndie dev and launchTrading and quant

Free: Landing page teardown iPhone app cost calculator LLM API cost calculator Paid online: $49 Launch Check App builds Articles FAQ