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
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.