8

I have a byte array which I would like to return as std::string::String. The other answers and docs I found were converting Vectors to strings.

How would I convert a byte array &[u8] to a String?

Qwertie
  • 5,784
  • 12
  • 45
  • 89

1 Answers1

10

It's working with std::str::from_utf8:

std::str::from_utf8(byte_array).unwrap().to_string();

Playground

Tim Diekmann
  • 7,755
  • 11
  • 41
  • 69
  • If you want a `String`, you can also use: `String::from_utf8(byte_array.to_vec()).unwrap()` – Venryx Mar 08 '22 at 18:38