# Method to append any \`Display\` type to a String

**URL:** <https://internals.rust-lang.org/t/method-to-append-any-display-type-to-a-string/17070>\
**Category:** libs\
**Created:** [July 23, 2022, 12:35pm UTC](https://internals.rust-lang.org/t/method-to-append-any-display-type-to-a-string/17070 "2022-07-23T12:35:31Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [July 24, 2022, 4:24pm UTC](https://internals.rust-lang.org/t/method-to-append-any-display-type-to-a-string/17070/21 "2022-07-24T16:24:32Z")

</div>

> [@cgwalters](#):
>
> But I think basically ever time I've wanted to write a `Display` to a `String` it's on a type I know for sure has an infallible `Display` impl.

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]`).

---

<div class="post-metadata">

**Author:** ![cgwalters](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cgwalters/32/1065_2.png) [@cgwalters](https://internals.rust-lang.org/u/cgwalters)\
**Post date:** [July 24, 2022, 7:57pm UTC](https://internals.rust-lang.org/t/method-to-append-any-display-type-to-a-string/17070/22 "2022-07-24T19:57:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [July 24, 2022, 8:12pm UTC](https://internals.rust-lang.org/t/method-to-append-any-display-type-to-a-string/17070/23 "2022-07-24T20:12:12Z")

</div>

> [@cgwalters](#):
>
> a bigger problem is that I think seeing a bunch of `unwrap` calls is usually a “code smell”

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

> <https://github.com/rust-lang/rust/blob/2a990f7c0fe4d00042269beb7ee67d0082de4e07/library/alloc/src/string.rs#L2485-L2492>

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.)

---

<div class="post-metadata">

**Author:** ![Kixunil](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kixunil/32/4110_2.png) [@Kixunil](https://internals.rust-lang.org/u/Kixunil)\
**Post date:** [August 3, 2022, 9:57am UTC](https://internals.rust-lang.org/t/method-to-append-any-display-type-to-a-string/17070/24 "2022-08-03T09:57:38Z")

</div>

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()`.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [November 1, 2022, 9:58am UTC](https://internals.rust-lang.org/t/method-to-append-any-display-type-to-a-string/17070/25 "2022-11-01T09:58:17Z")

</div>

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

[Previous page](https://internals.rust-lang.org/t/method-to-append-any-display-type-to-a-string/17070.md?page=1)
