Why isn't there a impl From<T: ToString> for String blanket impl?

I find it a bit strange that it doesn't exist. Is it omitted for a specific reason?

I can imagine two reasons.

First, without specialization, these impls would conflict with From<&str> and From<String>, for example.

Second, From::from() consumes its argument by value, which can be wasteful since ToString::to_string() only needs a pointer to the value being formatted. (This could probably be mitigated by implementing the trait for &T instead of T, although I'm not sure.)

2 Likes

To me, Into<String> means that a type is "string-like." Like, it contains some form of string data.

ToString is far more broad. It's implemented by all sorts of types, like integers and floats. I really think it would feel strange to call into() on one of these and obtain a String.

4 Likes

I second this. To me, the ToString trait signifies something which I can print to the screen to give a human-friendly message, whereas Into<String> is something that is naturally convertible to a String without loss of information.

3 Likes

This will makes it impossible on impl From<Box<str>> for String to reuse its allocation.

4 Likes