Does the 'generic blanket impl of ToString' conflict with the 'impl Tostring for char/str/String...' in rust

Viewed 124
1 Answers

Obviously it doesn't conflict since it works ☺

But you're right, it should conflict. There is a feature under development called "specialization" which will enable the compiler to resolve conflicts when multiple impl exist provided one is more specific than the others. In this case, implementing for an explicit type (char or String) is more specific than any generic impl<T> for T, so it works.

This feature is not yet stable enough for general use, but a subset of it is considered good enough for use in the Rust standard library, as you can see in the source code for impl ToString for char:

#[stable(feature = "char_to_string_specialization", since = "1.46.0")]
impl ToString for char {
Related