|
|
Subscribe / Log in / New account

Rust 1.60.0 released

Rust 1.60.0 released

Posted Apr 7, 2022 20:50 UTC (Thu) by tialaramex (subscriber, #21167)
Parent article: Rust 1.60.0 released

The change to Instant monotonicity promises were the right highlight for LWN because if there are people who see this problem they're probably LWN readers (I think I've read pieces about trying to make Linux guarantee this here). However if you have the comfort of writing software in a less fraught environment which doesn't experience time travel, there are two nice small safety improvements in the library here I think are worth a moment.

5.abs_diff(-10) == 15 means you can ask "These two (maybe signed) integers I have, how big is the difference between them?" and get an (unsigned) answer without spending ten minutes trying to figure out if your solution does anything unexpected in corner cases each time or worrying whether you're doing something needlessly slow.

Wrapping<T> getting OpAssign<T> implementations means e.g. Wrapping<i8> (a signed 8-bit integer which exhibits wrapping behaviour on overflow) has operators like += and *= working on i8 operands. So now:

let acc: Wrapping<i8> = 0;
let a: i8 = blackbox_100();
acc += a;
let b: i8 = blackbox_100();
acc += b;

... is a nice, syntax light way to express the fact you want the hardware two's complement behaviour for your accumulator acc, and you won't be surprised that acc is now -56 because that's how wrapping arithmetic works.

Rust's i8 will actually do this in release builds by default 'cos it's fast, but because overflow usually represents a programming error it will panic in debug builds when the overflow occurs, so Wrapping<i8> is the way to express that you deliberately want wrapping arithmetic and now it has a nicer affordance for this type of accumulation operation.


to post comments


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds