Avatar ·

How to convert a vector of bytes (Vec<u8>) to a string (String) in Rust?

Hello! I'm new to Rust and writing a small network client. After receiving data from a socket, I get a `Vec` that contains text data encoded in UTF-8. I need to convert this vector into a regular `String` to print it to the screen or store it in a variable. Here's an example of the code I currently have: ```rust fn main() { let bytes: Vec = vec![72, 101, 108, 108, 111]; // "Hello" in UTF-8 // How to convert bytes into String? // let text: String = ???; println!("{}", text); } ``` I tried using `bytes.to_string()`, but the compiler complains that the `ToString` trait is not implemented for `Vec`. I've also seen mentions of `String::from_utf8()`, but I'm not sure how to properly handle errors if the bytes are not valid UTF-8.