summaryrefslogtreecommitdiff
path: root/Saturating a value when wrapping.md
blob: 2cc5bf9c234f4e4d744fef38526fb76ee27fe078 (plain)
1
2
3
4
5
6
7
8
9
10
11
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`