join two strings in rust

In Rust, you can join two or more strings using the + operator, which concatenates the two strings. Here's an example:

main.rs
let str1 = "Hello";
let str2 = "World";
let result = str1 + str2;

println!("{}", result); // Output: HelloWorld
113 chars
6 lines

Alternatively, you can use the format!() macro to join multiple strings:

main.rs
let str1 = "Hello";
let str2 = "World";
let str3 = "!";
let result = format!("{} {}{}", str1, str2, str3);

println!("{}", result); // Output: Hello World!
156 chars
7 lines

In the above example, the two strings str1 and str2 are joined using the + operator, while the str3 is concatenated using string formatting.

related categories

gistlibby LogSnag