You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Auto merge of #39116 - mgattozzi:better-string-message, r=nrc
Add clearer error message using `&str + &str`
This is the first part of #39018. One of the common things for new users
coming from more dynamic languages like JavaScript, Python or Ruby is to
use `+` to concatenate strings. However, this doesn't work that way in
Rust unless the first type is a `String`. This commit adds a check for
this use case and outputs a new error as well as a suggestion to guide
the user towards the desired behavior. It also adds a new test case to
test the output of the error.
error[E0369]: binary operation `+` cannot be applied to type `&'static str`
2
+
--> $DIR/issue-39018.rs:12:13
3
+
|
4
+
12 | let x = "Hello " + "World!";
5
+
| ^^^^^^^^
6
+
|
7
+
note: `+` can't be used to concatenate two `&str` strings
8
+
--> $DIR/issue-39018.rs:12:13
9
+
|
10
+
12 | let x = "Hello " + "World!";
11
+
| ^^^^^^^^
12
+
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.
13
+
| let x = "Hello ".to_owned() + "World!";
14
+
15
+
error[E0369]: binary operation `+` cannot be applied to type `World`
16
+
--> $DIR/issue-39018.rs:17:13
17
+
|
18
+
17 | let y = World::Hello + World::Goodbye;
19
+
| ^^^^^^^^^^^^
20
+
|
21
+
note: an implementation of `std::ops::Add` might be missing for `World`
0 commit comments