It is possible to build up strings using the push and push_str methods on a mutable String, or using its + operator. However, it is often more convenient to use format!, especially where there is a mix of literal and non-literal strings.
Using write! on a String may not be quite as terse as the latter or as efficient as the former, but it's a pretty good (and generally more readable) alternative. It does require having std::fmt::Write in scope.
The
format!
idiom notes:Using
write!
on aString
may not be quite as terse as the latter or as efficient as the former, but it's a pretty good (and generally more readable) alternative. It does require havingstd::fmt::Write
in scope.