Method to append any `Display` type to a String

I think the unwrap just isn't that bad for these cases. If it's inlined it might be optimized away, but even if not, it's one obviously-unlikely branch (since the panicking path is #[cold]).

I should have said earlier that performance isn’t my biggest concern here; a bigger problem is that I think seeing a bunch of unwrap calls is usually a “code smell”. Just yesterday I fixed an unwrap in code I wrote (unrelated to Display stuff) to correctly use ? because we were aborting on legitimate regular errors.

1 Like

I was picturing the unwap being inside a library method, though, just like it is for .to_string():

That didn't get a special "can't fail Display" version, so I don't think this needs to either.

(Maybe there could be String::push_display, for example.)


Some of those APIs could be specialized on owned types to remove extra allocations though, so passing an owned value that's no longer needed may be a good thing. Even in case of push_fmt one could specialize on String to replace self if self.is_empty().

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.