Handle custom diagnostic for &str + String
This commit is contained in:
parent
1670a532dd
commit
20bc72e693
3 changed files with 61 additions and 27 deletions
|
|
@ -17,6 +17,9 @@ pub fn main() {
|
|||
// that won't output for the above string concatenation
|
||||
let y = World::Hello + World::Goodbye;
|
||||
//~^ ERROR cannot be applied to type
|
||||
|
||||
let x = "Hello " + "World!".to_owned();
|
||||
//~^ ERROR cannot be applied to type
|
||||
}
|
||||
|
||||
enum World {
|
||||
|
|
|
|||
|
|
@ -16,5 +16,19 @@ error[E0369]: binary operation `+` cannot be applied to type `World`
|
|||
|
|
||||
= note: an implementation of `std::ops::Add` might be missing for `World`
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
error[E0369]: binary operation `+` cannot be applied to type `&str`
|
||||
--> $DIR/issue-39018.rs:21:13
|
||||
|
|
||||
21 | let x = "Hello " + "World!".to_owned();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `+` can't be used to concatenate a `&str` with a `String`
|
||||
help: `to_owned()` can be used to create an owned `String` from a string reference. String concatenation appends the string on the right to the string on the left and may require reallocation. This requires ownership of the string on the left
|
||||
|
|
||||
21 | let x = "Hello ".to_owned() + "World!".to_owned();
|
||||
| ^^^^^^^^^^^^^^^^^^^
|
||||
help: you also need to borrow the `String` on the right to get a `&str`
|
||||
|
|
||||
21 | let x = "Hello " + &"World!".to_owned();
|
||||
| ^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: aborting due to 3 previous errors
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue