Rust 1.98.0: algebraic floating-point operations and fast number formatting

Translated by a model; the original is in Russian.

· Программы
The Rust team has released version 1.98.0. Among the new features are “algebraic” methods for floating-point numbers in the spirit of -ffast-math, buffered formatting of integers, and a stabilized guarantee regarding ManuallyDrop.

cover.svg

The Rust team has announced the stable release of version 1.98.0 of the language. As usual, you can update with the rustup update stable command.

Algebraic floating-point operations

The f32 and f64 types have gained “algebraic” methods for addition, subtraction, multiplication, division, and taking the remainder. They allow the compiler to apply optimizations based on the properties of real numbers — even though, in the world of the limited floating-point representation, those properties don't strictly hold. In spirit, this is close to the -ffast-math option found in other languages.

The classic example is associativity. An ordinary sum a + b + c + d must be evaluated by the compiler left to right: ((a + b) + c) + d. But if you write the same sum as a chain of algebraic_add calls, it's free to rearrange the operations — for instance, to compute (a + b) + (c + d) in parallel. Often this also opens the door to broader loop vectorization. The methods are nondeterministic — the compiler chooses the optimization itself — but they don't cause undefined behavior.

Fast integer formatting

All primitive integer types have acquired a format_into method that takes a &mut NumBuffer<Self> — a buffer guaranteed to hold the decimal representation of any value of that type. The method returns a ready-made &str borrowed from the buffer. This trick sidesteps a significant part of the dynamic dispatch inherent in formatting via write!, and according to measurements from the itoa-benchmark repository, it performs on par with itoa itself. Thus, format_into may become a standard replacement for this and similar dependencies.

A clarification regarding ManuallyDrop and Box

Up until version 1.96.0, the compiler harbored a bug that made moving a ManuallyDrop<Box<_>> after freeing its contents count as undefined behavior — the compiler forbids moving an already-freed Box, and ManuallyDrop inherited that restriction. In 1.96.0 the behavior was fixed, and now the documentation has been updated to guarantee that such code will not be UB in the future either. Details are in the ManuallyDrop documentation and RFC 3336.

Stabilized APIs

Among other things, the following have been stabilized: str::substr_range, [T]::subslice_range, NonZero::from_str_radix, the String::from_utf16{le,be} family and its lossy variants, strip_circumfix for strings and slices, and a number of Atomic<T> methods (from_mut, get_mut_slice, from_mut_slice). The complete list of changes to the language itself, Cargo, and Clippy is available in the detailed release notes.


Source: Lobsters

Comments

Sign in, to comment.

  • No comments yet.