std::mem::transmute
can be used for bit cast in Rust
let a: f32 = 42.42;
let frankentype: u32 = unsafe {
std::mem::transmute(a)
};
Why is Transmute Unsafe
I had a brain rot about why transmute is unsafe. This is because you can have a bit pattern that does not represent a valid value of the target type. This makes me wish that Rust has “conditional unsafe” for generic functions.
A comment from mastodon:
The
bytemuck
crate is commonly used for this