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"));
– Jessabell&String
allowed? – Aliciaalickpush_str()
accepts a&str
rather thanString
because it has to copy the data anyway, so it doesn't gain anything by consuming theString
it receives. And if you have an owned string, it's trivial to obtain a reference, as shown. – Dishabille