Eats Indigo
Eats Indigo

Reputation: 396

How does the `Display` trait auto implement the `ToString` trait in Rust?

I'm at Ch10-02 in the Rust Programming Lang which covers blanket implementations.

The docs for the Display trait state that:

Implementing this trait for a type will automatically implement the ToString trait for the type, allowing the usage of the .to_string() method. Prefer implementing the Display trait for a type, rather than ToString.

I understand that there is a statement somewhere in the source that looks something like this:

impl<T: Display> ToString for T {
    // --snip--
}

And I also understand that implementing Display automatically gets you ToString, but where is this linkage defined? It's unclear to me how you can tell when a blanket implementation is being applied.

Upvotes: 0

Views: 720

Answers (1)

Silvio Mayolo
Silvio Mayolo

Reputation: 70377

Generally, blanket implementations are defined with the trait that they're implementing, so in the case of impl<T: Display> ToString for T the blanket impl should appear alongside ToString. We can see this in the docs, which list just such an impl.

Upvotes: 1

Related Questions