Rust 的强制类型转换的时候要先转为离自己最近的类型的指针,再转为其他指针,然后 Drefer 得到新类型的变量。
struct A {
a: u64,
b: u64,
c: u64,
d: u64,
}
#[derive(Debug, PartialEq)]
struct B {
a: u8,
b: u8,
c: u8,
d: u8,
e: u64,
f: u64,
g: u64,
}
fn main() {
let a = A { a: 95, b: 96, c: 97, d: 98 };
let b = unsafe { & *(&a as *const A as * const B) };
assert_eq!(b, &B{a:98, b:0, c:0, d:0, e:95, f:96, g:97});
}