# Builtin for Vec\<u8\> to String

**URL:** https://users.rust-lang.org/t/builtin-for-vec-u8-to-string/41098
**Category:** uncategorized
**Created:** [April 16, 2020, 11:43pm UTC](https://users.rust-lang.org/t/builtin-for-vec-u8-to-string/41098 "2020-04-16T23:43:44Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![anon80458984](https://avatars.discourse-cdn.com/v4/letter/a/53a042/32.png) [@anon80458984](https://users.rust-lang.org/u/anon80458984)
#### Post date: [April 16, 2020, 11:43pm UTC](https://users.rust-lang.org/t/builtin-for-vec-u8-to-string/41098/1 "2020-04-16T23:43:44Z")

</div>

[String in std::string - Rust](https://doc.rust-lang.org/std/string/struct.String.html#method.from_utf8) is not what I want.

I want a literal translation of each u8 to a char and then to concatenate it into a string.

```rust
        let mut ans = String::new();
        for x in self.data.iter() {
            // x is a &u8
            ans.push(x.as_char());
        }

```

Is there a builtin for this?

---

<div class="post-metadata">

### Author: ![mbrubeck](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mbrubeck/32/648_2.png) [@mbrubeck](https://users.rust-lang.org/u/mbrubeck)
#### Post date: [April 16, 2020, 11:50pm UTC](https://users.rust-lang.org/t/builtin-for-vec-u8-to-string/41098/2 "2020-04-16T23:50:20Z")

</div>

If "literal translation" means you want to treat each u8 as a Unicode code point, then you can do this:

```rust
let ans: String = self.data.iter().map(|x| char::from(*x)).collect();

```

This effectively treats the input as ISO-8859-1 (Latin-1) encoded text, since the Unicode range from 0 to 255 is based on Latin-1. So for example the byte 255 will map to the character `'ÿ'`. (If your input data is not Latin-1 encoded, this is probably not what you want!)

[Playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=06594275728edcac5602883467a44a33)

---

<div class="post-metadata">

### Author: ![anon80458984](https://avatars.discourse-cdn.com/v4/letter/a/53a042/32.png) [@anon80458984](https://users.rust-lang.org/u/anon80458984)
#### Post date: [April 17, 2020, 3:35am UTC](https://users.rust-lang.org/t/builtin-for-vec-u8-to-string/41098/3 "2020-04-17T03:35:52Z")

</div>

> [@mbrubeck](#):
>
> (If your input data is not Latin-1 encoded, this is probably not what you want!)

Data is not Latin-1 encoded. This is precisely what I wanted. Thanks!

(Use case: I have a Vec which represents Ascii source. I'm trying to print it for debugging purposes.)

---

<div class="post-metadata">

### Author: ![Hyeonu](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/hyeonu/32/7970_2.png) [@Hyeonu](https://users.rust-lang.org/u/Hyeonu)
#### Post date: [April 17, 2020, 3:42am UTC](https://users.rust-lang.org/t/builtin-for-vec-u8-to-string/41098/4 "2020-04-17T03:42:23Z")

</div>

If your source is ASCII encoded, use `String::from_utf8`. UTF-8 is a superset of the ASCII so every valid ASCII text is also valid UTF-8 text. With this route you can save allocations and byte-by-byte transcoding.

If you don't want to convert the source buffer into `String` and just want to debug-print it for ASCII text, try [`bstr`](https://docs.rs/bstr/0.2.12/bstr/) crate which supports _conventionally_ UTF-8 encoded bytes, unlike the primitive `str` type which is _guaranteed_ to be UTF-8 encoded.

---

<div class="post-metadata">

### Author: ![mbrubeck](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mbrubeck/32/648_2.png) [@mbrubeck](https://users.rust-lang.org/u/mbrubeck)
#### Post date: [April 17, 2020, 3:54am UTC](https://users.rust-lang.org/t/builtin-for-vec-u8-to-string/41098/5 "2020-04-17T03:54:50Z")

</div>

Yeah, for ASCII data you should probably use `bstr`, or `str::from_utf8` or [`String::from_utf8_lossy`](https://doc.rust-lang.org/std/string/struct.String.html#method.from_utf8_lossy). This should be faster than treating it as Latin-1: since the input is already valid UTF-8, it won't need conversion or char-by-char iteration. Also, all of these avoid copying and allocation, some or all of the time. The `from_utf8` functions even have a [fast path](https://github.com/rust-lang/rust/pull/30740) for ASCII input.

---

<div class="post-metadata">

### Author: ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)
#### Post date: [July 16, 2020, 3:54am UTC](https://users.rust-lang.org/t/builtin-for-vec-u8-to-string/41098/6 "2020-07-16T03:54:51Z")

</div>

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