Rust

Rust

Made by DeepSource

Found transmute from floating point type to integer type RS-W1126

Bug risk
Minor

In Rust, the transmute function is used to reinterpret the bits of a value of one type as another type. Hence, both types must have the same size.

Compilation will fail if this is not guaranteed. Transmute is semantically equivalent to a bitwise move of one type into another. It copies the bits from the source value into the destination value.

Hence, transmuting from floating point to integer type is considered bad in Rust because it can lead to invalid in-memory representation and undefined behavior.

Consider using as cast if you really must cast between the two types and want expected results, aka rounded integer values.

Bad practice

fn main() {
    let x: f32 = 42.2;
    let y: i32 = unsafe { std::mem::transmute(x) };
}