Skip to content
Tutorials Center
My learning

Topics

Lesson 105 of 132 ยท Advanced Rust

Advanced Trait Techniques

Go deeper with Rust traits: blanket impls, extension traits, generic trait objects, dyn compatibility, specialization status and impl Trait in traits.

By now you can define traits, implement them and use them as bounds. This lesson is about the techniques that library authors use to make traits feel almost magical: implementing a trait for every type that meets a condition, adding methods to types you do not own, mixing generics with trait objects, and returning impl Trait from trait methods. These are the patterns behind ToString, Iterator::map, the Read and Write helpers and most of the ecosystem's extension APIs.

You should be comfortable with Traits, Trait Bounds, Trait Objects, Associated Types and the earlier Advanced Traits lesson (supertraits, fully qualified syntax, the orphan rule, marker traits and upcasting). Here we build on those.

Blanket implementations

A blanket implementation implements a trait for every type that satisfies some bound. Instead of impl Describe for i32 and impl Describe for String, you write one generic impl:

Rust A blanket impl over every Display type

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

The standard library does exactly this: impl<T: Display + ?Sized> ToString for T is why to_string() works on integers, strings, paths that implement Display, and your own types as soon as you implement Display. The same trick gives you Into from From (impl<T, U: From<T>> Into<U> for T) and IntoIterator for every Iterator.

The price: coherence

Rust requires coherence: for any type and trait there can be at most one implementation. A blanket impl claims every type that meets the bound, so it conflicts with any more specific impl that could overlap.

Rust Overlapping impls (fails on purpose)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

The compiler reports error[E0119]: conflicting implementations of trait `Describe` for type `i32` with two labels: "first implementation here" on the blanket impl and "conflicting implementation for i32" on the specific one. Rust refuses to pick one because it cannot know which you meant. The fix is to drop one of the impls, narrow the blanket impl with a more specific bound, or use a different trait. The idea of overriding a blanket impl for one type is called specialization and is covered below.

Because of this rule, adding a blanket impl to a published library is a breaking change: some downstream crate may already have implemented your trait for a concrete type. Think twice before publishing one.

A blanket impl is also subject to the orphan rule. You can write impl<T: MyTrait> OtherTrait for T only if OtherTrait is yours (or you are in the crate that owns the trait). You cannot write impl<T: MyTrait> Display for T, because both the trait and the type parameter are foreign from the compiler's point of view.

Extension traits

An extension trait adds methods to a type you do not own. You write a new trait with the methods, then implement it for the foreign type (the orphan rule is satisfied because the trait is local). Users call the methods after importing the trait with use.

Rust Extension traits for str and for any iterator

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

This is how crates like itertools add dozens of methods to Iterator, how futures adds StreamExt and how tokio adds AsyncReadExt. The recipe has three parts:

  1. A trait whose methods have default bodies (so the impl block can be empty).
  2. A supertrait or bound on the trait (trait IterExt: Iterator) so the defaults can call the base methods.
  3. A blanket impl impl<I: Iterator> IterExt for I {} to give it to everyone.

Three details deserve attention. impl StrExt for str implements it on the unsized type str, so it works on &str, String (through deref) and Box<str>. The convention is to name the trait <Thing>Ext. And since the compiler only considers traits that are in scope, the extension is opt-in: nothing changes for code that does not import it. If two imported traits provide the same method name, you must disambiguate with the fully qualified syntax from Advanced Traits.

Generic traits and trait objects

Traits can have type parameters (trait Handler<Request>) and associated types (type Output). Both can be used behind dyn, but you have to fill in every parameter.

Rust dyn Trait with a type parameter and an associated type

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

dyn Handler<String> and dyn Handler<u32> are different types; you cannot mix them in one Vec. If a trait needs to work with one request type per implementer, an associated type (type Request;) is the better design, because each type then implements the trait at most once and the compiler can infer the rest. Parameters suit traits that a type implements several times (From<A>, From<B>).

Forwarding impls for references and boxes

A function that takes impl Handler<String> will not accept &Echo or Box<dyn Handler<String>> unless the trait is implemented for those. Library traits usually add forwarding impls:

Rust Forwarding impls for &T and Box

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

The standard library does the same for Iterator (&mut I is an iterator), Read, Write and Fn closures. Without ?Sized on the type parameter the impl would be skipped for dyn Area, whose size is not known.

Object safety and where Self: Sized

A trait can become a trait object (dyn Trait) only if it is dyn compatible (older name: object safe). The main rules, simplified:

  • No method may return Self (the caller of a dyn value does not know the concrete type).
  • No method may have type parameters of its own (fn visit<V>(&self, v: V)), because the vtable would need a copy for every V.
  • Methods may not have Self in argument position by value, except in the receiver.
  • Associated constants and fn new() -> Self style functions without a receiver are not allowed.
  • The trait must not require Self: Sized as a supertrait.
Rust A trait that is not dyn compatible (fails on purpose)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

The error is error[E0038]: the trait `Shape` is not dyn compatible. The notes explain that the method scaled references the Self type in its return type. You do not have to give up the whole trait. Mark the offending method with where Self: Sized: it then exists only for concrete types and is excluded from the vtable, so the rest of the trait stays usable as dyn Shape.

Rust where Self: Sized keeps the trait usable as dyn

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Iterator uses this trick on map, filter and most of its provided methods, which is why &mut dyn Iterator<Item = T> works and still has next(). See Trait Objects for how the vtable works.

Specialization: the unfinished feature

The coherence conflict from earlier has a well-known wish attached: "let a more specific impl override a general one". That is specialization. It lets a library offer a fast path (for example copy_from_slice for Copy types, or a ToString that writes straight into a String for str) behind a generic interface.

As of Rust 1.99, specialization is still unstable, available only on nightly under #![feature(specialization)] (unsound in some corners) or the smaller #![feature(min_specialization)] that the standard library itself uses. It is not scheduled for stabilization soon, and you should not build a public library on it. What do you do instead?

  • Put the specialized behaviour in a different method or trait that the user chooses explicitly (to_vec versus clone_from_slice).
  • Use a newtype so the special case is a distinct type.
  • For 'static types, branch at run time with std::any::TypeId. The optimizer removes the dead branch because TypeId::of is a compile-time constant.
Rust Poor man's specialization with TypeId

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

This only works for 'static types and cannot reach into the value without a downcast through Any (Trait Objects), so use it as a last resort. The more idiomatic answer is almost always to make the special case a separate trait or function.

impl Trait inside traits

You already know impl Trait in argument position (a generic) and return position of free functions (an opaque type chosen by the function body). Since Rust 1.75 you may also write it in the return position of a trait method (RPITIT). Each implementer returns its own concrete type, and callers see only impl Iterator.

Rust Return-position impl Trait in a trait (Rust 1.75)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

There is one catch: a trait that uses impl Trait in a method return is not dyn compatible, because each implementer has a different hidden type. If you need both, return Box<dyn Iterator<Item = &str> + '_> instead, which costs one allocation but works behind dyn. async fn in traits (also 1.75) is the same feature underneath: it returns an impl Future.

What about impl Trait in an associated type?

The natural spelling for a named return type is type Titles = impl Iterator<Item = &str>; inside the impl. This is called type alias impl trait (TAIT) and it is still unstable (#![feature(impl_trait_in_assoc_type)] on nightly). Until it lands, you have three stable options when you need to name the type, for example to store it in a struct:

  1. Write the concrete type out (std::slice::Iter<'a, String>), perhaps behind a type alias.
  2. Use a boxed trait object, Box<dyn Iterator<Item = &'a str> + 'a>.
  3. Define a small struct that implements Iterator (Custom Iterators).

A related stable feature is generic associated types (GATs, Rust 1.65): associated types with their own lifetime or type parameters. They allow a "lending" iterator whose items borrow from the iterator itself, something Iterator cannot express.

Rust A lending iterator with a generic associated type

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

A normal Iterator cannot do this, because every item must be independent of the iterator once next returns. Here each window borrows the iterator for the length of one loop pass, and the compiler checks it. The cost is that GAT-heavy APIs are harder to read, so use them when the borrowed-item pattern is the point.

Choosing the right tool

Goal Reach for
Give a capability to every type with a property Blanket impl impl<T: Bound> Trait for T
Add methods to a foreign type or trait Extension trait, named FooExt
Store different types behind one interface dyn Trait (dyn compatible, where Self: Sized escape hatch)
Pass a reference or box where a trait is required Forwarding impl for &T / Box<T> with ?Sized
Different return type per implementer impl Trait in the trait method (1.75+) or a boxed trait object
Override a generic impl for one type Not possible on stable: separate trait, newtype or TypeId

Do not reach for clever traits first. A plain generic function or an enum is simpler to read and to debug. Blanket impls and extension traits pay off in libraries with many users; in application code a short function usually does the job.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does impl<T: Display> Describe for T {} do?

    It is a blanket implementation: every type satisfying the Display bound gets Describe, just as ToString works for all Display types.
  2. 2. Why does adding a more specific impl Describe for i32 next to the blanket impl fail?

    Specialization would allow it, but it is unstable. On stable Rust the overlap is rejected with error[E0119].
  3. 3. What does where Self: Sized on a trait method achieve?

    Methods that return Self or are generic would break dyn compatibility. With where Self: Sized they are callable only on concrete types.
  4. 4. Which is the usual recipe for an extension trait on all iterators?

    Default method bodies do the work, the supertrait lets them call Iterator methods, and the blanket impl gives the trait to every iterator.
  5. 5. What is true of impl Trait in the return position of a trait method (Rust 1.75)?

    Each implementer returns its own hidden type, which is a stable feature. Because the type differs per implementer, the trait cannot be used as dyn.

Summary

  • A blanket impl (impl<T: Bound> Trait for T) gives a trait to every type meeting the bound; coherence forbids overlapping impls, so it is hard to add to a published library.
  • Extension traits add methods to foreign types: default-bodied methods, a supertrait, and a blanket impl; callers opt in with use.
  • Generic and associated-type traits work behind dyn only when every parameter is named; forwarding impls for &T and Box<T> (with ?Sized) make them ergonomic.
  • A trait is dyn compatible only without Self-returning or generic methods; where Self: Sized removes individual methods from the vtable.
  • Specialization is unstable on Rust 1.99; use separate traits, newtypes or TypeId instead.
  • impl Trait in trait method returns is stable since 1.75 (not dyn compatible); impl Trait in associated types is unstable; GATs (1.65) express lending iterators.

Next: Pin, Unpin and Self-Referential Types