57. New Math Constants — GOLDEN_RATIO and EULER_GAMMA in std
Tired of defining your own golden ratio or Euler-Mascheroni constant? As of Rust 1.94, std ships them out of the box — no more copy-pasting magic numbers.
Before: Roll Your Own
If you needed the golden ratio or the Euler-Mascheroni constant before Rust 1.94, you had to define them yourself:
| |
This works, but it’s error-prone. One wrong digit and your calculations drift. And every project that needs these ends up with its own slightly-different copy.
After: Just Use std
Rust 1.94 added GOLDEN_RATIO and EULER_GAMMA to the standard consts modules for both f32 and f64:
| |
These sit right alongside the constants you already know — PI, TAU, E, SQRT_2, and friends.
Where You’d Actually Use Them
Golden ratio shows up in algorithm design (Fibonacci heaps, golden-section search), generative art, and UI layout proportions:
| |
Euler-Mascheroni constant appears in number theory, harmonic series approximations, and probability:
| |
The Full Lineup
With these additions, std::f64::consts now includes: PI, TAU, E, SQRT_2, SQRT_3, LN_2, LN_10, LOG2_E, LOG2_10, LOG10_2, LOG10_E, FRAC_1_PI, FRAC_1_SQRT_2, FRAC_1_SQRT_2PI, FRAC_2_PI, FRAC_2_SQRT_PI, FRAC_PI_2, FRAC_PI_3, FRAC_PI_4, FRAC_PI_6, FRAC_PI_8, GOLDEN_RATIO, and EULER_GAMMA. That’s a pretty complete toolkit for numerical work — all with full f64 precision, available at compile time.