concat two strings and trim the result in rust

To concat two string in Rust, we can use the + operator. To trim the resulting string, we can use the trim() method on the resulting string. Here's an example:

main.rs
fn main() {
    let str1 = "Hello";
    let str2 = " World!";

    let result = (str1.to_owned() + str2).trim();

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

In this example, we first define two string variables str1 and str2. We then concatenate these two strings using the + operator and store the resulting string in a new variable called result. Finally, we use the trim() method to remove any leading or trailing whitespaces from the resulting string.

gistlibby LogSnag