Using format!
, I can create a String
from a format string, but what if I already have a String
that I'd like to append to? I would like to avoid allocating the second string just to copy it and throw away the allocation.
let s = "hello ".to_string();
append!(s, "{}", 5); // Doesn't exist
A close equivalent in C/C++ would be snprintf
.
I see now that
String
implementsWrite
, so we can just usewrite!
:(Playground)
It is impossible for this
write!
call to return anErr
, at least as of Rust 1.23, so theunwrap
should not cause concern.