// 使用std::any::type_name可以打印数据类型
fn print_type_of<T>(_: &T) {
println!("type is = {}", std::any::type_name::<T>())
}
- impl copy
❯ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.42s
Running `target/debug/guessing_game`
type is = alloc::string::String
str2 is: "tosei"
type is = alloc::string::String
str is: "tosei"
type is = &[u8]
str1_bytes is: [116, 111, 115, 101, 105]
type is = [u8; 5]
str1_num is: [116, 111, 115, 101, 105]
type is = alloc::vec::Vec<u8>
vec_from_slice is [116, 111, 115, 101, 105]
type is = alloc::vec::Vec<u8>
str1_vec is [116, 111, 115, 101, 105]
type is = alloc::string::String
str_from_vec is "tosei"
type is = alloc::vec::Vec<u8>
vec_from_string is [116, 111, 115, 101, 105]
type is = u32
bytes_u32 is 203435063
- struct
use std::rc::Rc;
#[derive(Debug)]
struct Transfer {
trans: Rc<Vec<u8>>,
message: String,
}
impl Transfer {
fn new(trans: &[u8], message: &str) -> Self {
Transfer {
trans: Rc::new(trans.to_owned()),
message: message.to_owned(),
}
}
fn trans(&self) -> Vec<u8> {
self.trans.clone().to_vec()
}
fn message(&self) -> String {
self.message.clone()
}
}
fn main() {
let transfer = Transfer::new(b"tosei", "hello");
let trans = transfer.trans();
let message = transfer.message();
// Vec -> [u8]
let trans_array: [u8; 5] = trans.try_into().unwrap();
println!("trans_array is: {:?}", trans_array);
// String -> [u8]
let message_array: [u8; 5] = message.as_bytes().try_into().unwrap();
println!("message_array is: {:?}", message_array);
// [u8] -> &str
let trans_str = std::str::from_utf8(&trans_array).unwrap();
println!("trans_str is: {:?}", trans_str);
}
❯ cargo run
Compiling guessing_game v0.1.0 (/Users/tosei/workspace/practice/rust/rust-p/rust_small_demo)
Finished dev [unoptimized + debuginfo] target(s) in 0.82s
Running `target/debug/guessing_game`
trans_array is: [116, 111, 115, 101, 105]
message_array is: [104, 101, 108, 108, 111]
trans_str is: "tosei"