Abstractions in Practice · 16 min · free lesson

Blanket Implementations

Implement behavior for every matching type.

Core idea

A generic impl can grant a trait to all types satisfying a bound.

A Display blanket rule grants Named behavior to several matching types but excludes a type without Display.
One generic implementation applies to every type satisfying its bound.

Understand it

A generic impl can grant a trait to all types satisfying a bound. Blanket implementations are powerful because they shape which future implementations remain legal under coherence rules.

A blanket implementation defines one rule for every type satisfying a bound. In the example, any sized T that implements Display also receives Named, whose name() delegates to to_string(). This avoids writing essentially the same implementation for each printable type. The benefit is broad, but so is the commitment: every current and future Display type covered by the rule gets that behavior.

Trace a call on an integer and one on a custom struct with Display. Both resolve to the same generic implementation, specialized for their concrete type. A type without Display has no Named implementation from this blanket rule, so a caller demanding Named cannot use it. The implicit Sized bound on T also leaves out str itself, unless the impl adds ?Sized. This is a compile-time capability relationship, not a runtime registry. The code does not inspect the type's name or perform a dynamic check.

Blanket implementations interact with coherence. If your crate already grants Named to all Display types, a more specific Named implementation for one overlapping Display type conflicts on stable Rust. Also, the orphan rules limit which trait/type combinations your crate may implement. Before publishing a broad blanket impl, ask whether future special cases should behave differently. Try adding an overlapping implementation and read the compiler error. Sometimes a newtype wrapper or a narrower bound gives users more room to customize behavior later.

As a design exercise, imagine a type whose Display format changes over time. Would the generated name remain appropriate? A blanket rule is strongest when the mapping from the bound to the new behavior is stable for all covered types.

Read the code

trait Named { fn name(&self) -> String; }
impl<T: std::fmt::Display> Named for T {
    fn name(&self) -> String {
        self.to_string()
    }
}

The example makes the contract visible in code.

Practise this code in Playground ↗

Try it

Say which types get Named from this impl, and which do not.

What success looks like: You can name one type that receives name() and one that does not, and say why.

Check your understanding

Who receives Named in this impl?

Reveal the answer
  1. Only String
  2. Every Sized Display type
  3. Only local types

Correct: Every Sized Display type.

Go deeper

Common misconception: “A type only gets Named by writing its own impl Named.”

No opt-in exists. This one impl gives Named to every sized T with Display, including i32 and String. A type without Display gets nothing from it.

  1. Read the bound: impl<T: std::fmt::Display> Named for T covers every sized T with Display, and name() just calls self.to_string().
  2. Predict before checking: Predict what 5_i32.name() and String::from("crab").name() return. Then predict what happens on a struct without Display.
  3. Compare with evidence: Both return their Display text, 5 and crab, from one impl. The plain struct gets E0599, because its Display bound is not satisfied.

Transfer challenge: Add impl Named for String beside the blanket impl and read E0119. Then remove it, change the bound to T: std::fmt::Display + ?Sized, and check that fn show<T: Named + ?Sized>(v: &T) now accepts a &str.

Connections

  • Trait Objects: Choose behavior at runtime with dyn Trait
  • Default Trait Methods: Share behavior while allowing an override
Learn this lesson with AI

Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.

I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools.
Lesson: Blanket Implementations (courseId "ownership", lessonId "blanket-impls")
Link: https://crabrix.com/learn/ownership/blanket-impls/
Goal: A generic impl can grant a trait to all types satisfying a bound.
Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=ownership/blanket-impls) and I will write the code there.
Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.

In Claude Code, Codex or Cursor, paste it into a chat with Crabrix Academy connected. On a phone, the Claude and ChatGPT apps open the same links.