If I have two String
s, how do I append one to the other using stable Rust? It's a simple question but stackoverflow doesn't think I am verbose enough apparently. This is some extra text.
Asked
Active
Viewed 156 times
0
-
Ever heard of google? https://doc.rust-lang.org/book/strings.html#concatenation – moffeltje Jul 01 '15 at 08:56
-
1Yes I tried googling but that did not come up. Also that isn't mentioned in the reference documentation for `String`. – Timmmm Jul 01 '15 at 08:58
-
1You can try googling with spesific keyword like this : `string concatenation rust` . It works, trust me :)) – Iswanto San Jul 01 '15 at 09:00
-
@Timmmm I don't know anything about rust but I just googled it and picked the most reliable source and BAM! there is the solution :-) – moffeltje Jul 01 '15 at 09:02
1 Answers
2
From here
If you have a String, you can concatenate a &str to the end of it:
let hello = "Hello ".to_string(); let world = "world!";
let hello_world = hello + world;
But if you have two Strings, you need an &:
let hello = "Hello ".to_string();
let world = "world!".to_string();
let hello_world = hello + &world;

Community
- 1
- 1

Iswanto San
- 18,263
- 13
- 58
- 79
-
Ah yes, it was the automatic coersion from `String` to `&str` using `&` that I didn't know about. I was trying to use `as_str()` but it isn't available yet. Thanks! (Also might be worth mentioning `push_str()`. – Timmmm Jul 01 '15 at 08:58
-
Note that `to_string` on `&str`s is inefficient. Use `into` or `to_owned` instead. – Veedrac Jul 01 '15 at 11:08