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.
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
View all Courses