rsut 字节数组和字符串转换
一、字符串转换为字节数组
let s = String::from("str");
let v = s.as_bytes(); // &[u8]
println!("{:?}", v);
// 结果:[115, 116, 114]
let s = String::from_utf8_lossy(v);
println!("{:?}", s);
// 结果:str
二、字节数组转换为字符串
let s = String::from("str");
let v = s.as_bytes(); // &[u8]
let v1 = a.as_bytes(); // Vec<u8>
println!("{:?}", v);
// 结果:[115, 116, 114]
let s = String::from_utf8_lossy(v);
let s1 = String::from_utf8(v1);
println!("{:?}", s);
// 结果:str
println!("{:?}", s1);
// 结果:str