Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Concatenate strings using format!

  • format!

  • In this case all the strings are copied so it is less efficient than where we move the string, but this means we can continue to use the original variables.

fn main() {
    let string1 = String::from("Apple");
    let string2 = String::from("Banana");
    let str3 = "Peach";

    let text = format!("{}-{}-{}-{}", string1, string2, str3, "other");
    println!("{}", text);
    println!("{}", string1);
    println!("{}", string2);
    println!("{}", str3);
}
Apple-Banana-Peach-other
Apple
Banana
Peach