All posts

Rust / Implement external traits for external types

Posted On 01.14.2022

One of the rules for working with traits in Rust is: You are not allowed to implement a trait on a type if either the trait or the type is not local to your crate.

For example, within your program, both the trait fmt::Display and type Vec are external because they’re defined in the standard library. So, you cannot do something like:

impl fmt::Display for Vec<String> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

Compiling the code above will give us the following error:

error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
 --> src/
3 | impl fmt::Display for Vec<String> {
  | ^^^^^^^^^^^^^^^^^^^^^^-----------
  | |                     |
  | |                     `Vec` is not defined in the current crate
  | impl doesn't use only types from inside the current crate
  = note: define and implement a trait or new type instead
For more information about this error, try `rustc --explain E0117`.

One way to go about this problem is to create a new type in a tuple struct that wraps around the type you want to implement a trait for.

Since the wrapper type is local to our crate, we are allowed to implement any trait for it. We can access the inner type within the new type implementation with self.0 (which mean the first item of the tuple, since the new type is a tuple):

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

Because List is the new type that wraps around Vec<String>, you cannot directly access Vec<String>‘s methods. You can re-implement these methods and redirect the call to self.0, or try implement Deref trait for it.