I have two variables, each containing a string. I want to concatenate both with a linefeed in between them. How do I do that?
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.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments