Safety of transmute
⚓ Rust 📅 2025-12-18 👤 surdeus 👁️ 1Is this code safe?
use std::marker::PhantomData;
struct Foo<T> {
bar: u8,
baz: PhantomData<T>,
}
impl<T> Foo<T> {
fn new(bar: u8) -> Self {
Self { bar, baz: PhantomData }
}
}
fn main() {
let v = Foo::<&mut i32>::new(4);
let x = unsafe {
// don't focus on i32 and bool, I've chosen two random types
std::mem::transmute::<Foo<&mut i32>, Foo<&mut bool>()
};
}
3 posts - 3 participants
🏷️ Rust_feed