What FRAC_2_SQRT_PI does
FRAC_2_SQRT_PI is a constant exposed by the std::f128::consts 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.
FRAC_2_SQRT_PI 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::f128::consts::FRAC_2_SQRT_PI; bring it into scope with use std::f128::consts::FRAC_2_SQRT_PI; or refer to it by its full path.
When to use it
Reference FRAC_2_SQRT_PI from std::f128::consts 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 FRAC_2_SQRT_PI constant
use std::f128::consts::FRAC_2_SQRT_PI;
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 = FRAC_2_SQRT_PI;
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 FRAC_2_SQRT_PI in control flow
use std::f128::consts::FRAC_2_SQRT_PI;
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 < FRAC_2_SQRT_PI 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.