Does unsafe Rust have strict aliasing rules?

In C/C++, dereferencing a pointer that aliases another pointer of a different type is generally undefined behaviour because of the strict aliasing rule.

For example, the following C++ code (taken from cppreference) invokes undefined behaviour:

double d = 0.1;
std::int64_t n;
n = *reinterpret_cast<std::int64_t*>(&d);

My question is whether unsafe Rust also has strict aliasing rules. Would the following analogous Rust code also invoke undefined behaviour?

let d = 0.1f64;
let n: i64;
unsafe { n = *(&d as *const f64 as *const i64) }

Edit: Removed use of std::mem::transmute for pointer cast.

Posted Oct 3, 2017, 12:54 AM UTC · edited

2860
Loading comments…