I have two variables, each containing a string. I want to concatenate both with a linefeed in between them. How do I do that?
Asked
Active
Viewed 1,611 times
-1
-
1@Shepmaster Please, read the question. Your answer is wrong, I am not asking the same question in the link. – mrossini Jan 16 '17 at 10:01
-
Concatenating strings is concatenating strings... – Shepmaster Jan 16 '17 at 14:02
-
@Shepmaster Yes, concatenating strings is concatenating string, however that was not the question and your answer really did not help, even the link, so if you do not want to help, just don't. – mrossini Jan 16 '17 at 19:20
-
A question does not have to be exactly the same in order to be marked as duplicate. In this case, the linked question should definitely fulfill all string concatenation needs in Rust. If you do not agree, you should instead explain properly why the suggested question didn't help. Showing what you have tried so far to solve the problem also helps. You may wish to keep these tips in mind in the future. – E_net4 - Krabbe mit Hüten Jan 17 '17 at 17:32
-
@E_net4 in this case it does not solve the problem, see my answer below for Simon. Linefeed is not a string and Simon second answer was what I was trying to do with and the only thing I found in here. He was able to provide a decent and new answer that solved mu problem instead of wasting everyone time marking an answer as duplicate when it is NOT, despite what you think. – mrossini Jan 18 '17 at 08:18
-
Sure, this is what I (and possibly many other people) would think: a line feed can be turned into a string with little memory footprint: "\n", and the duplicate does mention the use of `format!()`. Nevertheless, you were expected to back your thoughts and misunderstandings into the question. Without that, a duplicate is not only appropriate, but what actually wastes less of everyone's time. – E_net4 - Krabbe mit Hüten Jan 18 '17 at 12:02
-
This is not a duplicate. Many concatenation methods (like `format!` macro) perform an escaping on the newline characters. This is a specific case – Michele Federici Nov 14 '18 at 09:45
1 Answers
6
There are a couple of ways.
The nicest I have seen is using the join method on an array:
fn main() {
let a = "Hello";
let b = "world";
let result = [a, b].join("\n");
print!("{}", result);
}
Depending on your use case you might also prefer more control:
fn main() {
let a = "Hello";
let b = "world";
let result = format!("{}\n{}", a, b);
print!("{}", result);
}
There are some more manual ways (some of which I believe avoid any allocations at all) but I prefer the above two.
Simon Whitehead
- 60,641
- 8
- 104
- 133
-
1
-
-
For some reason I cannot understand, second suggestion prints just one line as "Hello\nworld" , while first suggestion prints Hello in one line and world in the second. – mrossini Jan 19 '17 at 17:54
-
this is a word for word copy of the second answer here: https://stackoverflow.com/questions/30154541/how-do-i-concatenate-strings – andy boot Jan 24 '18 at 14:46
-