liveBook · Manning

Blanket implementation

As explained in Learn Rust in a Month of Lunches

A blanket implementation is an implementation of a trait for every type satisfying specified bounds or implementing another trait.

A blanket implementation gives a trait’s methods to every type matching an implementation’s target. An unconstrained generic target such as T includes every Rust type; bounds such as T: Debug narrow the set. section 17.6

A blanket implementation can:

  • Provide related behavior automatically: Display supplies ToString, From supplies Into, and Into supplies TryFrom. section 17.6

Converting external errors

A ToSystemError trait converts any Result<T, E> where E: std::error::Error into Result<T, SystemError>. Calling .to_system_error()? then places errors from parsing, UTF-8 conversion, or sending into ExternalCrateError.

How Learn Rust in a Month of Lunches covers it

How deep it goes
Defines generic targets, trait bounds, automatic trait relationships, and error conversion, but does not develop coherence rules.
Examples
Demonstrates SaysHello for every type, ToSystemError for Result values, and standard conversions involving Display, From, Into, and TryFrom.
What it assumes
Assumes familiarity with traits and generic types while teaching blanket implementations through unconstrained and bounded examples.
What you can do after
Apply it via Rust trait implementations to add shared methods broadly or normalize errors from functions returning different error types.
How it connects
Connects traits with generics, trait bounds, Display, ToString, From, Into, TryFrom, and error handling.

Also explained in

Compare how 3 books explain blanket implementation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Rust in a Month of Lunches. The section links point to the parts of the book it draws on.