`+` cannot be used to concatenate two `&str` strings
时间: 2023-11-21 11:14:13 浏览: 109
解决Python 异常TypeError: cannot concatenate 'str' and 'int' objects
The `+` operator can be used to concatenate two `&str` strings in Rust. The `+` operator performs the concatenation by converting one of the `&str` strings into an owned `String` type and then concatenating the two strings together.
Here's an example:
```rust
fn main() {
let str1 = "Hello,";
let str2 = " world!";
let result = str1.to_string() + str2;
println!("{}", result);
}
```
Output:
```
Hello, world!
```
In this example, the `str1` is first converted into an owned `String` using the `to_string()` method. Then the `+` operator is used to concatenate the converted string with `str2`. The resulting string is stored in the `result` variable and printed to the console.
阅读全文