6

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
  • 4,517
  • 10
  • 41
  • 78

1 Answers1

7

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

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

Playground

Tim Diekmann
  • 6,764
  • 10
  • 34
  • 58
  • If you want a `String`, you can also use: `String::from_utf8(byte_array.to_vec()).unwrap()` – Venryx Mar 08 '22 at 18:38