Closed
Description
For the following code:
fn main() {
let s: String = &String::from("hello");
let r: String = &format!("hello");
}
We get the following diagnostics:
error[E0308]: mismatched types
--> src/main.rs:2:21
|
2 | let s: String = &String::from("hello");
| ^^^^^^^^^^^^^^^^^^^^^^
| |
| expected struct `std::string::String`, found reference
| help: consider removing the borrow: `String::from("hello")`
|
= note: expected type `std::string::String`
found type `&std::string::String`
error[E0308]: mismatched types
--> src/main.rs:3:21
|
3 | let r: String = &format!("hello");
| ^^^^^^^^^^^^^^^^^
| |
| expected struct `std::string::String`, found reference
| help: try using a conversion method: `&format!("hello").to_string()`
|
= note: expected type `std::string::String`
found type `&std::string::String`
error: aborting due to 2 previous errors
Notice that the String expression correctly suggests removing the borrow, but the format!
expression incorrectly suggests adding to_string()
, which does not fix the error.