How do I print a vector of u8 as a string?

前端 未结 4 1159
误落风尘
误落风尘 2021-02-19 01:45

Here\'s my code:

let mut altbuf: Vec = Vec::new();

// Stuff here...

match stream.read_byte() {
    Ok(d) => altbuf.push(d),
    Err(e) => { pri         


        
4条回答
  •  [愿得一人]
    2021-02-19 02:06

    To print bytes as a UTF-8 string, use std::str::from_utf8 when the bytes may be malformed. Use the unsafe std::str::from_utf8_unchecked when the bytes are always valid UTF-8.

    println!("{}", std::str::from_utf8(&altbuf).unwrap());
    

提交回复
热议问题