考虑下面的代码:
use std::boxed::Box;
use std::mem::transmute;
trait Total {
fn total(&self) -> i32;
}
#[derive(Debug)]
struct S {
a: i32,
b: i32,
c: i32,
}
impl S {
fn new() -> S {
S { a: 2, b: 3, c: 4 }
}
}
impl Total for S {
fn total(&self) -> i32 {
self.a + self.b + self.c
}
}
fn main() {
let b: Box<Total> = Box::new(S::new());
unsafe {
let s: Box<S> = std::mem::transmute(b);
println!("S = {:?}", s);
}
}
这会导致错误:
error[E0512]: transmute called with differently sized types: Box<Total> (128 bits) to Box<S> (64 bits)
--> src/main.rs:30:29
|
30 | let s: Box<S> = std::mem::transmute(b);
| ^^^^^^^^^^^^^^^^^^^
考虑到 Box<Total>
实际上是一个 Box<S>
,为什么我们会收到这个错误提示?
let b: Box<Total>
? - Aurora0001