当前位置:   article > 正文

rust中String,&str,Vec <u8>和&[u8]的惯用转换_rust &[u8]转&str

rust &[u8]转&str

 转载自:https://zhuanlan.zhihu.com/p/372082802

作者:爬一手好线杆

  1. &str -> String--| String::from(s) or s.to_string() or s.to_owned()
  2. &str -> &[u8]---| s.as_bytes()
  3. &str -> Vec<u8>-| s.as_bytes().to_vec() or s.as_bytes().to_owned()
  4. String -> &str----| &s if possible* else s.as_str()
  5. String -> &[u8]---| s.as_bytes()
  6. String -> Vec<u8>-| s.into_bytes()
  7. &[u8] -> &str----| s.to_vec() or s.to_owned()
  8. &[u8] -> String--| std::str::from_utf8(s).unwrap(), but don't**
  9. &[u8] -> Vec<u8>-| String::from_utf8(s).unwrap(), but don't**
  10. Vec<u8> -> &str----| &s if possible* else s.as_slice()
  11. Vec<u8> -> String--| std::str::from_utf8(&s).unwrap(), but don't**
  12. Vec<u8> -> &[u8]---| String::from_utf8(s).unwrap(), but don't**
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/753354
推荐阅读
相关标签
  

闽ICP备14008679号