Rust学习笔记_2

类型转换记录

&[8]转Vec

1
2
3
4
5
6
// 三种将&[u8]转换为Vec<u8>的方法
let tmp_slice = &[1, 2, 3];

let tmp_vec: Vec<u8> = tmp_slice.iter().map(|p| p.to_owned()).collect();
let tmp_vec: Vec<u8> = tmp_slice.iter().cloned().collect();
let tmp_vec: Vec<u8> = tmp_slice.to_vec();

Vec转String

1
2
3
let tmp_vec = vec![240, 159, 146, 150];

let sparkle_heart = String::from_utf8(tmp_vec).unwrap();

String转i32

1
2
3
let tmp_string = String::from("12");

let tmp_int = tmp_string.parse::<i32>().unwrap();
  • Copyrights © 2019-2024 Klusfq
  • Visitors: | Views: