What MANTISSA_DIGITS does

MANTISSA_DIGITS is a constant exposed by the std::f32 module of the Rust standard library. It is part of the public, stable API and you can rely on it from any edition of Rust.

MANTISSA_DIGITS is a compile-time constant. Its value is computed once at compile time and inlined into the binary at every use site. Constants are always immutable and cannot be borrowed mutably. The full canonical path is std::f32::MANTISSA_DIGITS; bring it into scope with use std::f32::MANTISSA_DIGITS; or refer to it by its full path.

When to use it

Reference MANTISSA_DIGITS from std::f32 when you need its compile-time value. Constants are inlined at every use site, so they have no runtime memory cost beyond the bytes of the value itself.

Annotated examples

Reading the MANTISSA_DIGITS constant

use std::f32::MANTISSA_DIGITS;

fn demo() {
    // Constants are evaluated at compile time and inlined wherever they
    // appear. They have no runtime memory cost beyond the bytes of the
    // value itself.
    let value = MANTISSA_DIGITS;
    println!("{:?}", value);
}

Constants are zero-overhead — they're baked into the resulting binary at every use site. Use them for thresholds, limits, well-known string identifiers, and mathematical constants.

For deeper background, see the canonical Rust patterns reference for the broader context behind this section.

Comparing against MANTISSA_DIGITS in control flow

use std::f32::MANTISSA_DIGITS;

fn within_limit(value: u64) -> bool {
    // A constant is just a value — use it anywhere a literal would work,
    // and update the constant in one place to change every site at once.
    value < MANTISSA_DIGITS as u64
}

Centralising magic numbers in named constants pays for itself the first time you need to tune one — every reference updates atomically and the name documents intent.

Common pitfalls

Constants are evaluated at compile time and inlined at every use site — changing a constant requires a recompile of every dependent crate. For values that should be runtime-configurable, use a static or read from the environment.

For deeper background, see an in-depth Rust idioms cheat sheet for the broader context behind this section.

Performance & threading notes

Inlined at every use site. The compiler may deduplicate identical constants across translation units. There is no runtime allocation or initialisation cost.