Concatenate string literal with another string

concatenation, rust, string

Solution

By default string literals have static lifetime, and it is not possible to concatenate unique and static vectors. Using unique literal string helped:

fn main() {
    let x = ~"abcd";
    io::println(~"Message: " + x);
}

Problem

Is there some reason why I cannot concatenate a string literal with a string variable? The following code: ``` fn main() { let x = ~"abcd"; io::println("Message: " + x); } ``` gives this error: ``` test2.rs:3:16: 3:31 error: binary operation + cannot be applied to type `&'static str` test2.rs:3 io::println("Message: " + x); ^~~~~~~~~~~~~~~ error: aborting due to previous error ``` I guess this is a pretty basic and very common pattern, and usage of `fmt!` in such cases only brings unnecessary clutter.

Original source