How to Concatenate Strings in Rust

Concatenate strings in Rust using the format! macro or push_str method to join text efficiently.

Use the format! macro to concatenate strings without taking ownership, or the + operator if you own the first string.

let s1 = String::from("Hello");
let s2 = String::from("World");
let s3 = format!("{} {}", s1, s2);
println!("{s3}");

Alternatively, use push_str to append a string slice to an owned String:

let mut s = String::from("Hello");
s.push_str(" World");
println!("{s}");