#[derive(Debug)]
pub struct Test {
pub field: String,
}
impl Test {
pub fn change(&self) {
unsafe {
let mutable_t: &mut Test = &mut *(self as *const Self as *mut Self);
mutable_t.field = "changed".into();
}
}
}
fn main() {
let t = Test {
field: "default".into(),
};
println!("{:?}", t);
t.change();
println!("{:?}", t);
}
运行结果如下,field的值被改变了:
Test { field: "default" }
Test { field: "changed" }