How to properly concatenate strings in Rust: concat, format! or something else?
Hello everyone! I just started learning Rust, and I encountered a seemingly simple problem — string concatenation. In other languages (Python, JavaScript) I just wrote `"a" + "b"` and it worked. But in Rust the `+` operator behaves strangely.
Here's my code:
```rust
let hello = "Hello";
let world = "World";
let message = hello + " " + world; // Error!
```
The compiler gives an error: `cannot add `&str` to `&str``. I read that I need to use `to_string()`, but then the code becomes cumbersome:
```rust
let message = hello.to_string() + " " + world; // Works, but ugly
```
What if I have many strings, for example, a list of names? Or need to insert a number? I tried `format!`, but heard it's slow. Tell me, how is string concatenation done in Rust? What are the ways and which one is
No answers yet.
Log in to leave an answer
Blogs
Concurrency and Multithreading in JavaScript: A Complete Guide