fn main() {
let mut str = "string".to_string();
// How do I push String to String?
str.push_str(format!("{}", "new_string"));
}
So, how do I push the formatted string in str? I don't want to concatenate. Concatenation is kinda' the same thing as push but I want to know how I can push String instead.
str.push_str(&format!("{}", "new_string"));&Stringallowed?push_str()accepts a&strrather thanStringbecause it has to copy the data anyway, so it doesn't gain anything by consuming theStringit receives. And if you have an owned string, it's trivial to obtain a reference, as shown.