How do I convert a Vector of bytes (u8) to a string
问题 I am trying to write simple TCP/IP client in Rust and I need to print out the buffer I got from the server. How do I convert the u8 vector to a String for printing? 回答1: To convert a slice of bytes to a string slice (assuming a UTF-8 encoding): use std::str; // // pub fn from_utf8(v: &[u8]) -> Result<&str, Utf8Error> // // Assuming buf: &[u8] // fn main() { let buf = &[0x41u8, 0x41u8, 0x42u8]; let s = match str::from_utf8(buf) { Ok(v) => v, Err(e) => panic!("Invalid UTF-8 sequence: {}", e), }