News for hackers. Stuff that matters.
Re:Almost Always Unsigned by masklinn on Sunday January 02, 2022 @08:39AM (#29768614)
Though you can configure your release builds to panic as well. That can be quite useful if you don't expect trivial computations to be a large cost center (and you can always bench to check that assumption).

Even more so now that custom profiles have been added to Cargo (yay), you can enable overflow checking in release, and add a `release-unchecked` or whatever.

Re:Almost Always Unsigned by tialaramex on Sunday January 02, 2022 @10:05AM (#29769240)
Or, better in many cases, you can write what you meant explicitly in Rust.

3u64.checked_sub(4u64).expect("Overflow");

This allows you to spell out that you don't expect this to overflow, and Rust should panic if it does, regardless of your compiler flags.

Re:Almost Always Unsigned by masklinn on Sunday January 02, 2022 @10:09AM (#29769272)
Of course you can do that, but I would say that doing so defensively for every operation is way more verbose and unwieldy than you'd want.

Much simpler to just toggle on the relevant flag.

Re:Almost Always Unsigned by eyelidlessness on Sunday January 02, 2022 @11:39AM (#29770201)
I think GP was saying you can do that when you expect that the general case doesn’t need to be checked but a specific case does.
Re:Almost Always Unsigned by masklinn on Sunday January 02, 2022 @11:54AM (#29770387)
> when you expect that the general case doesn’t need to be checked but a specific case does.

But surely that's almost never the case? Few programs actually desire or care for base 2 modular arithmetics, and when they do it tends to be for very specific tasks (usually cryptographic or cryptography-adjacent e.g. hashing, checksumming, ...).

Re:Almost Always Unsigned by eyelidlessness on Sunday January 02, 2022 @01:12PM (#29771637)
> But surely that's almost never the case?

Which is why it’s not so impractical to make an occasional exception.

Re:Almost Always Unsigned by deepsun on Monday January 03, 2022 @03:45AM (#29778645)
I wish they had "unchecked_sub()" instead, and default "-" would work like checked_sub().
Re:Almost Always Unsigned by tialaramex on Monday January 03, 2022 @05:01AM (#29779092)
unchecked_sub() exists in Nightly and so may some day come to stable

However, changing - to perform checked_sub() leaves you with an Option, and this makes ordinary arithmetic look pretty clumsy:

let x = (a-b).unwrap()-c;

The intent is to land more types with intrinsic behaviour. Today only the Wrapping type is provided, so Wrapping<i32> is an i32 that definitely has Wrapping arithmetic and won't panic on overflow, but eventually Saturating<i16> will be possible (e.g. for CD audio PCM samples, saturating arithmetic is correct, if you try to make the loudest possible noise louder it just stays the same) and so will Unchecked<i8> if you really sure that you're doing 8-bit arithmetic that can't overflow and doesn't need Debug checks.