/** * changing the interpretation of bits without changing the bits in Rust. * adapted from https://tourofrust.com/96_en.html * ggtowell * Nov 4, 2023 */ fn main() { let a : u64 = 88887768; // pointer_a is a raw pointer. That is, it is a memory address // getting memory address is totally safe let raw_pointer_a = &a as *const u64; // type needs to agree with what 'a' started as println!("Data memory location: {:?} normally will get the value{}", raw_pointer_a, &a); // Turning our number into a raw pointer to a f32 is // also safe to do. let pointer_b = raw_pointer_a as *const f32; // again, 32 bits! let b = unsafe { // This is unsafe because we are telling the compiler // to assume our pointer is a valid f32 and // dereference it's value into the variable b. // Rust has no way to verify this assumption is true. // NOTE rust may panic if the dereference tries to read more bits than // were in the original a. // Compiler will not dereference a raw pointer unless wrapped in "unsafe" block *pointer_b }; println!("I swear this is a pie! {}", b); }