Since "localhost" isn't assigned to a variable, couldn't it be a safe implicit conversion?
So this is fine:
let host: &str = "localhost";
Because a &str is just a read-only pointer to some characters [1], and you're just setting host to be a pointer to those characters in the read-only section.But this is not:
let host: String = "localhost";
Because a String is not just a pointer, it's a pointer which implies ownership of an allocation on the heap [1]. That means you can modify the contents of a String, and when the String is dropped, that allocation will be freed. You can't just set up a String using a pointer into the read-only section. You need to make an allocation, copy the characters into it, and use that. Which is what str::to_string() does.[1] And a length, but we can ignore that for now.