资讯详情

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

转载自: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**

标签: vec2r7505qg超级电容

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

 锐单商城 - 一站式电子元器件采购平台  

 深圳锐单电子有限公司