Rust

Rust

Made by DeepSource

Found transmute between tuple and array or slice RS-E1032

Bug risk
Major

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.

As tuples don't have a fixed layout, transmuting from a tuple type to array or slice is considered undefined behaviour.

Consider using pattern matching.

Bad practice

fn foo() {
    let zeroed_array: [u64; 2] = unsafe { std::mem::transmute((0u64, 0u64)) };
}

Recommended

fn foo() {
    let zeroed_array: [u64; 2] = match (0u64, 0u64) {
        (x, y) => [x, y]
    };
}