9951 explained code solutions for 126 technologies


rustHow to convert a Rust slice of u8 to a string?


To convert a Rust slice of u8 to a string, you can use the str::from_utf8 function. This function takes a slice of bytes and returns a Result<&str, Utf8Error>.

Example code

let bytes = [104, 101, 108, 108, 111];
let string = str::from_utf8(&bytes).unwrap();

Output example

hello

The code above does the following:

  1. let bytes = [104, 101, 108, 108, 111]; - creates a slice of bytes
  2. let string = str::from_utf8(&bytes).unwrap(); - calls the str::from_utf8 function with the &bytes slice as an argument, and then unwraps the Result to get the &str

Helpful links

  • str::from_utf8 - official documentation for the str::from_utf8 function

Edit this code on GitHub