From 931eb0894e7cd3717f1218f0eb06382b11734c13 Mon Sep 17 00:00:00 2001 From: Jasper Ras Date: Sun, 3 Aug 2025 22:29:47 +0200 Subject: vault backup: 2025-08-03 22:29:47 --- Saturating a value when wrapping.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 Saturating a value when wrapping.md (limited to 'Saturating a value when wrapping.md') diff --git a/Saturating a value when wrapping.md b/Saturating a value when wrapping.md new file mode 100644 index 0000000..2cc5bf9 --- /dev/null +++ b/Saturating a value when wrapping.md @@ -0,0 +1,12 @@ +[[Integers can overflow if compiled with --release]] +[[Rust]] + +This was a bit confusing to me, but [this forum post](https://users.rust-lang.org/t/saturating-what-does-it-really-do-and-when-is-it-useful/52720/2) sums it up quite well: + +Instead of wrapping around or error'ing, the maximum or minimum value of the type is chosen depending on which is closest to the result. + +Example with `u8`: +`0 - 1 = 0` +`7 - 8 = 0` +`8 + 1 = 8` +`1 + 8 = 8` \ No newline at end of file -- cgit v1.2.3