> ## 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.

# Rust u128

`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 2<sup>128</sup> - 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.

```rust theme={"dark"}
// 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.

```rust theme={"dark"}
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:

```rust theme={"dark"}
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`

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Rust Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
