Home / Docs / Standard library / Built-in methods

saturating_add

Release v0.4.0Edition 0.2 and later

A built-in method, resolved by the type of its receiver (spec/14 §2). Edition 0.2 and later.

una
a.saturating_add(by: F) -> F
  where a: F, one of I8, I16, I32, I64, U8, U16, U32, U64

Total: the exact result clamped to the type's range (spec/18 §5).

Receiver

Parameters

Returns

F: the exact sum clamped to the range of the type: the largest value when the sum is above it, the smallest when it is below; it never fails.

See also

wrapping_add

Example

src/shop.unauna
module shop

--- A one-byte stock level that stops at the largest and the smallest value.
pub main() -> Unit
  uses @console: Console[print]
=
  let stock: U8 = 250
  let full = stock.saturating_add(by: 10)
  let bottom: I8 = -128
  let floor = bottom.saturating_add(by: -1)
  @console.print("{full}")
  @console.print("{floor}")

una run . prints:

Output
255
-128

Specification

Generated from una v0.4.0 (docs/std/builtins.json, which is not part of the specification): built-in methods have no declaration, so una writes these signature lines and tests each against its checker. The specification governs: spec/14 §2.