Rust 里 String 如何转换为 &str
Rust 里 String 如何转换为 Vec
Rust 里 String 如何转换为 Vec
或其他相互转换,请看:
use std::str;
fn main() {
// 起始:Vec let src1: Vec<char> = vec!['j','{','"','i','m','m','y','"','}'];
// 从 Vec let string1: String = src1.iter().collect::<String>();
// 从 Vec let str1: &str = &src1.iter().collect::<String>();
// 从 Veclet byte1: Vec<u8> = src1.iter().map(|c| *c as u8).collect::<Vec<_>>();
//输出 println!("Vec<char>:{:?} | String:{:?}, str:{:?}, Vec<u8>:{:?}", src1, string1, str1, byte1);
// 起始:Vec
// in rust, this is a slice
// b - byte, r - raw string, br - byte of raw string let src2: Vec<u8> = br#"e{"ddie"}"#.to_vec();
// 从 Vec
// from_utf8 以utf8方式转换 let string2: String = String::from_utf8(src2.clone()).unwrap();
// 从 Veclet str2: &str = str::from_utf8(&src2).unwrap();
// 从 Vec let char2: Vec<char> = src2.iter().map(|b| *b as char).collect::<Vec<_>>();
//输出println!("Vec<u8>:{:?} | String:{:?}, str:{:?}, Vec<char>:{:?}", src2, string2, str2, char2);
// 起始为一个 String let src3: String = String::from(r#"o{"livia"}"#);
// 直接变为一个&strlet str3: &str = &src3;
// 从 String 转换为Vec let char3: Vec<char> = src3.chars().collect::<Vec<_>>();
// 从String转换为Vec let byte3: Vec<u8> = src3.as_bytes().to_vec();
//Print println!("String:{:?} | str:{:?}, Vec<char>:{:?}, Vec<u8>:{:?}", src3, str3, char3, byte3);
// 起点为 &strlet src4: &str = r#"g{'race'}"#;
// 从&str转换为String let string4 = String::from(src4);
//从&str 转换为 Vechlet char4: Vec<char> = src4.chars().collect();
// 从 &str 转换为 Veclet byte4: Vec<u8> = src4.as_bytes().to_vec();
//输出println!("str:{:?} | String:{:?}, Vec<char>:{:?}, Vec<u8>:{:?}", src4, string4, char4, byte4);
}
参考:
https://gist.github.com/jimmychu0807/9a89355e642afad0d2aeda52e6ad2424