expression

Prefer safe floating-point bit conversion

Use the dedicated bit-conversion methods to interpret a floating-point encoding. They express the desired operation without unsafe. This example uses a finite exactly representable number; do not assume arbitrary bit patterns give finite or ordinary numbers. Expected stdout (not executed): true true Common mistake: Using a numeric as cast to decode an IEEE floating-point bit pattern.

Formal Syntax & Structure

rust syntaxrust
f32::from_bits(bits); value.to_bits()

Working Example

Example Snippetrust
#![deny(unsafe_op_in_unsafe_fn)]fn main() {    let value = f32::from_bits(0x4000_0000);    println!("{} {}", value == 2.0, value.to_bits() == 0x4000_0000);}
Rust Reference