Abstractions in Practice · 16 min · free with an account

Blanket Implementations

Lesson preview · about 30%

Implement behavior for every matching type.

Core idea

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

Start with the idea

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 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.

Keep learning, for free

The rest of this lesson is waiting for you.

Create a free account to read the complete explanation, see the infographic, explore the code, and try the exercises. All 742 lessons and algorithm steps are included.

No payment details. Your account opens the complete Academy.

← All lessons in Ownership Mastery