# Const OsString::new, PathBuf::new

**URL:** <https://internals.rust-lang.org/t/const-osstring-new-pathbuf-new/21758>\
**Category:** libs\
**Created:** [October 24, 2024, 6:47am UTC](https://internals.rust-lang.org/t/const-osstring-new-pathbuf-new/21758 "2024-10-24T06:47:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![zk4x](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/zk4x/32/12656_2.png) [@zk4x](https://internals.rust-lang.org/u/zk4x)\
**Post date:** [October 24, 2024, 6:47am UTC](https://internals.rust-lang.org/t/const-osstring-new-pathbuf-new/21758/1 "2024-10-24T06:47:07Z")

</div>

Can std::ffi::os\_str::OsString::new as well as std::path::PathBuf::new be const fn? Afaik they use String as inner and String::new is already const, so there shouldn't be any reason why they can't be const.

Thank you.

---

<div class="post-metadata">

**Author:** ![quinedot](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/quinedot/32/7294_2.png) [@quinedot](https://internals.rust-lang.org/u/quinedot)\
**Post date:** [October 24, 2024, 7:59am UTC](https://internals.rust-lang.org/t/const-osstring-new-pathbuf-new/21758/2 "2024-10-24T07:59:50Z")

</div>

(Not really relevant to your request, but) they don't use `String` because they support non-UTF-8.

---

<div class="post-metadata">

**Author:** ![jhpratt](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jhpratt/32/11640_2.png) [@jhpratt](https://internals.rust-lang.org/u/jhpratt)\
**Post date:** [October 24, 2024, 10:26am UTC](https://internals.rust-lang.org/t/const-osstring-new-pathbuf-new/21758/3 "2024-10-24T10:26:23Z")

</div>

A quick look at the internals reveals no technical reason this cannot be implemented. Windows uses WTF-8, which is `Vec<u8>` plus a `bool` indicating if it's known to be UTF-8. Everything else uses `Vec<u8>` (that is UTF-8).

---

<div class="post-metadata">

**Author:** ![Nemo157](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nemo157/32/11585_2.png) [@Nemo157](https://internals.rust-lang.org/u/Nemo157)\
**Post date:** [October 24, 2024, 12:29pm UTC](https://internals.rust-lang.org/t/const-osstring-new-pathbuf-new/21758/4 "2024-10-24T12:29:17Z")

</div>

> [@jhpratt](#):
>
> Everything else uses `Vec<u8>` (that is UTF-8).

A superset of UTF-8.

---

<div class="post-metadata">

**Author:** ![chrisd](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/chrisd/32/7232_2.png) [@chrisd](https://internals.rust-lang.org/u/chrisd)\
**Post date:** [November 1, 2024, 3:05pm UTC](https://internals.rust-lang.org/t/const-osstring-new-pathbuf-new/21758/5 "2024-11-01T15:05:05Z")

</div>

Creating empty string-like containers should always be able to be const so I see no reason `OsString` and `PathBuf` should be different whatever their internal representation.

Aside: on all platforms, `OsStr` must be a superset of UTF-8 due to the existing conversion methods being documented as zero cost (e.g. `&str` -\> `&OsStr`). Therefore for all platform it will always be possible to have a const UTF-8 path even if non UTF-8 requires some special handling.

---

<div class="post-metadata">

**Author:** ![quinedot](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/quinedot/32/7294_2.png) [@quinedot](https://internals.rust-lang.org/u/quinedot)\
**Post date:** [November 1, 2024, 3:56pm UTC](https://internals.rust-lang.org/t/const-osstring-new-pathbuf-new/21758/6 "2024-11-01T15:56:48Z")

</div>

> [@chrisd](#):
>
> Aside: on all platforms, `OsStr` must be a superset of UTF-8

[The promise is a bit stronger than that even.](https://doc.rust-lang.org/std/ffi/os_str/struct.OsStr.html#method.from_encoded_bytes_unchecked)

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [April 25, 2026, 3:56pm UTC](https://internals.rust-lang.org/t/const-osstring-new-pathbuf-new/21758/7 "2026-04-25T15:56:49Z")

</div>

Closed on April 25, 2026, 3:56pm UTC

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