这个函数可以直接读取或者转化为二进制数据,适用于系统编程;
use std::mem;
struct Testdata{
ucc:u8,
udd:u8,
}
fn main() {
let ddd = Testdata{ucc:2,udd:9};
unsafe{
let uu16:u16=mem::transmute_copy(&ddd);
println!("{}",uu16%256);
println!("{}",uu16>>8);
let uu8:[u8;2]=mem::transmute_copy(&ddd);
println!("{}",uu8[0]);
println!("{}",uu8[1]);
}
println!("Hello, world!");
}
输出
warning: `bytebyte` (bin "bytebyte") generated 1 warning
Finished dev [unoptimized + debuginfo] target(s) in 0.47s
Running `target\debug\bytebyte.exe`
2
9
2
9
Hello, world!
标签:转换,uu8,二进制,bytebyte,let,println,mem,Rust,ddd
From: https://www.cnblogs.com/hardfood/p/17461217.html