Equivalent to absl::StrAppend

I'm building up a string by appending numbers, but my current code has a bunch of wasteful intermediate allocations, specifically:

    s.push_str(&n.to_string());

The equivalent absl::StrAppend API uses the string's buffer directly and avoids the intermediate allocation.

If n is itself an &str, then you can simply do s.push_str(n).

String implements fmt::Write, so you can use write!(&mut s, "{}", n) - this should avoid the excess allocations.

4 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.