What are blanket implementations

Guys, in documentation I see section called "blanket implementation". Could anyone please tell me what is it?
Thanks

11 Likes

It is an implement of a trait either for all types, or for all types that match some condition. For example, the stdandard library has this impl:

impl<T> ToString for T
where
    T: Display + ?Sized,
{ ... }

It is a blanket impl that implements ToString for all types that implement the Display trait.

18 Likes

Thanks, could you please tell me what ?Sized means?

1 Like

Rust adds the T: Sized bound to all generics by default. Adding ?Sized removes that bound, allowing non-sized types too.

5 Likes

To clarify, the Sized trait means we know something's size at compile time. If you ever want to store something as a local variable the compiler needs to know how much space to set aside, and adding a ?Sized to your where clause relaxes that restriction.

See Dynamically Sized Types (DSTs) from The Nomicon if you want to go further down the ?Sized rabbit hole.

8 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.