转载自:https://zhuanlan.zhihu.com/p/372082802
作者:爬一手好线杆
&str -> String--| String::from(s) or s.to_string() or s.to_owned()
&str -> &[u8]---| s.as_bytes()
&str -> Vec<u8>-| s.as_bytes().to_vec() or s.as_bytes().to_owned()
String -> &str----| &s if possible* else s.as_str()
String -> &[u8]---| s.as_bytes()
String -> Vec<u8>-| s.into_bytes()
&[u8] -> &str----| s.to_vec() or s.to_owned()
&[u8] -> String--| std::str::from_utf8(s).unwrap(), but don't**
&[u8] -> Vec<u8>-| String::from_utf8(s).unwrap(), but don't**
Vec<u8> -> &str----| &s if possible* else s.as_slice()
Vec<u8> -> String--| std::str::from_utf8(&s).unwrap(), but don't**
Vec<u8> -> &[u8]---| String::from_utf8(s).unwrap(), but don't**
本文详细介绍了Rust中不同字符串类型如`&str`, `String`, `&[u8]`和`Vec<u8>`之间的转换方法,包括使用`as_bytes()`, `to_string()`, `into_bytes()`等函数。请注意在进行UTF-8编码和解码时的正确使用方式。
695

被折叠的 条评论
为什么被折叠?



