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

Mar*_*ten 27 type-conversion rust

:像我这样的新Rustacean杂耍这些类型的斗争String,&str,Vec<u8>,&[u8].

随着时间的推移,我希望有一个顿悟,并突然得到一些库调用使用其中一个的原因.在那之前,我需要帮助来确定每个惯用过渡.

鉴于以下类型:

let st: &str = ...;
let s:  String = ...;
let u:  &[u8] = ...;
let v:  Vec<u8> = ...;
Run Code Online (Sandbox Code Playgroud)

我想我已经弄清了这些,但它们是惯用的吗?

&str    -> String    String::from(st)
&str    -> &[u8]     st.as_bytes()
String  -> &str      s.as_str()
&[u8]   -> &str      str::from_utf8(u)
Vec<u8> -> String    String::from_utf8(v)
Run Code Online (Sandbox Code Playgroud)

最终,我想要一个完整的转换表,这些类型:

&str    -> String
&str    -> &[u8]
&str    -> Vec<u8>
String  -> &str
String  -> &[u8]
String  -> Vec<u8>
&[u8]   -> &str
&[u8]   -> String
&[u8]   -> Vec<u8>
Vec<u8> -> &str
Vec<u8> -> String
Vec<u8> -> &[u8]
Run Code Online (Sandbox Code Playgroud)

oli*_*obk 39

&str

  • &str -> String许多同样有效的方法:String::from(st),st.to_string(),st.to_owned().
    • 但我建议你在一个项目中坚持使用其中一个.主要优点String::from是您可以将其用作map方法的参数.所以代替x.map(|s| String::from(s))你经常使用x.map(String::from).
  • &str- > &[u8]完成了st.as_bytes()
  • &str- > Vec<u8>是组合&str -> &[u8] -> Vec<u8>,即st.as_bytes().to_vec()

st.as_bytes().to_owned()

  • String应该只是String -> &str在可以获得强制的&s地方,或者在没有强制的地方.
  • s.as_str()与以下相同String -> &[u8]:&str -> &[u8]
  • s.as_bytes() 有一个自定义方法: String -> Vec<u8>

s.into_bytes()

  • &[u8] 完成了 &[u8] -> Vec<u8>
  • u.to_owned()实际上并不存在,也就是说u.to_vec(),通过提供to_vec
  • &[u8] -> Result<&str, Error> 是的组合 str::from_utf8(u)

&[u8] -> Result<&str, Error> -> Result<String, Error>

  • String::from_utf8(u).unwrap()应该只是Result::map强制可用的地方,或者Vec<u8>不是强制的地方.
  • Vec<u8> -> &[u8]&vie一样as_slice
  • Vec<u8> -> &[u8] -> Result<&str, Error>实际上并不存在,那将是str::from_utf8(&v)通过str::from_utf8(&v).unwrap()

只要目标不是通用的,而是明确地分类为Vec<u8> -> Result<String, Error>String::from_utf8(v)分别,就可以使用强制


TL;博士

&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**

* target should have explicit type (i.e., checker can't infer that)

** handle the error properly instead
Run Code Online (Sandbox Code Playgroud)

  • 如果其他人想知道你可以通过 `String::from_utf8_lossy(&amp;*s)` 将 Vec&lt;u8&gt; 转换为有损字符串 (3认同)