# Pre-RFC - allow usage of str::utf8\_char\_width

**URL:** <https://internals.rust-lang.org/t/pre-rfc-allow-usage-of-str-utf8-char-width/15748>\
**Category:** libs\
**Created:** [December 8, 2021, 12:56pm UTC](https://internals.rust-lang.org/t/pre-rfc-allow-usage-of-str-utf8-char-width/15748 "2021-12-08T12:56:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SadiinsoSnowfall](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/sadiinsosnowfall/32/8495_2.png) [@SadiinsoSnowfall](https://internals.rust-lang.org/u/SadiinsoSnowfall)\
**Post date:** [December 8, 2021, 12:56pm UTC](https://internals.rust-lang.org/t/pre-rfc-allow-usage-of-str-utf8-char-width/15748/1 "2021-12-08T12:56:46Z")

</div>

Hi!

While writing a decoder for small binary messages in the context of an embedded device, I needed at some point to try to decode a single utf8-encoded character at the begining of a u8 buffer. I encountered a few problems: I could not decode the entire buffer as a utf8-encoded str, since only the first bytes (first character, up to 4 bytes) were known to be valid utf8 code points. One way to solve this problem is to perform utf8 decoding only on the first character by first extracting a sub-slice containing only the first bytes. However, I needed to know the length of the first character; I saw that a function to get this length was available in the stdlib ([`str::utf8_char_width`](https://doc.rust-lang.org/core/str/fn.utf8_char_width.html)), but I couldn't use it because it is part of the `str_internals` feature group, which is compiler-only. I ended up just copying the code from the function and everything worked.

I know this use case is really uncommon, but I think it would be a good idea to make this function available. After all, the opposite function [char::len\_utf8](https://doc.rust-lang.org/std/primitive.char.html#method.len_utf8) (which gives the length in bytes required when encoding a character using utf8) is already available and stable.

---

<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:** [December 8, 2021, 1:16pm UTC](https://internals.rust-lang.org/t/pre-rfc-allow-usage-of-str-utf8-char-width/15748/2 "2021-12-08T13:16:57Z")

</div>

For the time being you can use [decode\_utf8 in bstr - Rust](https://docs.rs/bstr/latest/bstr/fn.decode_utf8.html), or just copy the short definition of `utf8_char_width` from the standard library.

---

<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:** [March 8, 2022, 1:17pm UTC](https://internals.rust-lang.org/t/pre-rfc-allow-usage-of-str-utf8-char-width/15748/3 "2022-03-08T13:17:40Z")

</div>

Closed on March 8, 2022, 1:17pm UTC

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