Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

u128 is a primitive data type in Rust representing an unsigned 128-bit integer. It occupies exactly 16 bytes of memory and stores non-negative whole numbers ranging from 0 up to 2128 - 1.

Technical Specifications

  • Memory Size: 128 bits (16 bytes).
  • Minimum Value: 0 (accessible via u128::MIN).
  • Maximum Value: 340,282,366,920,938,463,463,374,607,431,768,211,455 (accessible via u128::MAX).
  • Memory Alignment: Target-architecture dependent, but typically aligned to 16-byte boundaries on modern 64-bit systems.

Syntax and Initialization

You can instantiate a u128 using explicit type annotation, literal suffixes, or standard numeric separators (_) for readability.
// Explicit type annotation
let dec_val: u128 = 1_000_000_000_000_000;

// Literal suffix (explicitly dictates the type)
let suffixed_val = 42_u128;

// Hexadecimal representation (exactly 32 hex digits for max value)
let hex_val = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_u128;

// Binary representation
let bin_val = 0b101010_u128;

Memory Layout and Endianness

Because u128 spans 16 bytes, its byte-level representation is subject to the endianness of the target architecture. Rust provides built-in methods to explicitly convert u128 values to and from byte arrays ([u8; 16]) in specific byte orders.
let val: u128 = 0x0123456789ABCDEF0123456789ABCDEF;

// Convert to Little-Endian byte array
let le_bytes: [u8; 16] = val.to_le_bytes();

// Convert to Big-Endian (Network Byte Order) byte array
let be_bytes: [u8; 16] = val.to_be_bytes();

// Convert to Native-Endian byte array
let ne_bytes: [u8; 16] = val.to_ne_bytes();

// Reconstruct from byte array
let reconstructed = u128::from_be_bytes(be_bytes);

Overflow and Arithmetic Operations

Like all Rust integer primitives, u128 panics on integer overflow in debug builds and performs two’s complement wrapping in release builds. To enforce specific overflow behavior regardless of the build profile, Rust provides explicit arithmetic methods:
let max = u128::MAX;

// Wrapping: Overflows wrap around to 0
let wrapped = max.wrapping_add(1); // 0

// Checked: Returns an Option, None on overflow
let checked = max.checked_add(1); // None

// Saturating: Clamps to the numeric bound on overflow
let saturated = max.saturating_add(1); // u128::MAX

// Overflowing: Returns a tuple (result, boolean_flag)
let (result, did_overflow) = max.overflowing_add(1); // (0, true)

Trait Implementations

The u128 type natively implements standard library traits required for memory safety, concurrency, mathematical operations, and logical operations:
  • Core Semantics: Copy, Clone (enabling bitwise copy semantics rather than move semantics)
  • Concurrency: Send, Sync (safe to transfer and share across thread boundaries)
  • Hashing: Hash
  • Arithmetic: Add, Sub, Mul, Div, Rem
  • Bitwise: BitAnd, BitOr, BitXor, Shl (Shift Left), Shr (Shift Right)
  • Comparison: Eq, PartialEq, Ord, PartialOrd
  • Formatting: Display, Debug, UpperHex, LowerHex, Binary, Octal
Master Rust with Deep Grasping Methodology!Learn More