Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

rust
jargon

Definition: Blanket implementation

Educative Answers Team

With implementations in Rust, you can extend the functionality of an implementation type. Implementations are defined with the impl keyword and contain functions that belong to an instance of a type, statically, or to an instance that is being implemented.

svg viewer
With blanket implementations you can save writing similar implementations for multiple types

What are blanket implementations?

Blanket implementations leverage Rust’s ability to use generic parameters. They can be used to define shared behavior using traits. This is a great way to remove redundancy in code by reducing the need to repeat the code for different types with similar functionality.

In the code below, we are making a blanket implementation on a generic type, T, that implements the Display trait.

impl<T: Display> ToString for T {
    // ...
}

To elaborate, our generic type, T, is bound to implement Display. Therefore, we use behavior guaranteed by the Display type, to produce a string representation, to our advantage.

Read more about blanket implementations in the official docs.

RELATED TAGS

rust
jargon
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring