Avatar ·

How to properly concatenate strings in Rust: a complete guide for beginners

Hello, colleagues! I've just started learning Rust and encountered a problem that has me quite puzzled. I need to concatenate several strings into one, but I can't figure out how to do it correctly, and I keep getting compilation errors.\n\nHere's an example of my code:\n\n```rust\nfn main() {\n let hello = "Hello";\n let world = "world";\n let message = hello + " " + world;\n println!("{}", message);\n}\n```\n\nThe compiler gives an error: `cannot add `&str` to `&str``. I've tried different approaches: using `format!`, `push_str`, but I'm still confused. In other languages, like Python or JavaScript, I just write `"Hello" + " " + "world"` and it works. But in Rust there are so many different types: `String`, `&str`, `String::from`, `to_string()`. When should I use which?\n\nI need to concatenate not