Skip to main content
i128 is a 128-bit signed integer primitive type in Rust. It occupies 16 bytes of memory and represents values using two’s complement binary encoding, providing the largest native integer size available in the Rust standard library.

Value Range

Because it is a signed 128-bit integer, its range is defined mathematically as 2127-2^{127} to 212712^{127} - 1.
  • Minimum (i128::MIN): -170,141,183,460,469,231,731,687,303,715,884,105,728
  • Maximum (i128::MAX): 170,141,183,460,469,231,731,687,303,715,884,105,727

Syntax and Initialization

You can declare i128 values using type inference, explicit type annotation, or the i128 literal suffix. Rust allows the _ character as a visual separator to manage the high digit count.

Memory Layout and Endianness

i128 is stored as a contiguous 16-byte sequence. The byte order (endianness) depends on the target architecture, but Rust provides explicit methods to serialize and deserialize i128 into byte arrays ([u8; 16]) regardless of the host platform’s native endianness.

Arithmetic and Overflow Behavior

Standard arithmetic operators (+, -, *, /, %) are implemented for i128. By default, integer overflow causes a panic in debug builds and performs two’s complement wrapping in release builds. To enforce specific overflow behaviors programmatically, i128 exposes explicit arithmetic methods:

Type Conversion (Casting)

Casting between i128 and other primitive types is handled via the as keyword or the From/Into traits.
  • Widening: Converting smaller integers (e.g., i64, u32) to i128 is lossless and can be done safely using From.
  • Narrowing: Casting i128 to smaller integers using as truncates the higher-order bits, which alters the value and potentially the sign if the truncated value exceeds the target type’s capacity.
Tired of Poor Rust Skills? Fix That With Deep Grasping!Learn More