Why does the `ToString` trait exist?

Why was the to_string() method not simply added to the Display trait?

1 Like

Sadly, it's more like a historical accident but we can't remove it due to the backward compatibility.

Mostly because Display is in core, and String is in alloc, so Display cannot reference String

17 Likes

The deeper reason is that Display is polymorphic. Dumping to a String is one implementation, but Display also works with non-allocating output objects.

2 Likes

Polymorphic Display does not prevent one from adding a fn to_string(&self) -> String { ... } default implementation. The fact that String is in another crate does.

7 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.