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

`u16` is a primitive unsigned 16-bit integer type in Rust. It occupies exactly 2 bytes of memory and represents non-negative whole numbers ranging from 0 to 65,535 ($2^{16} - 1$). Because it is unsigned, it cannot represent negative values and uses all 16 bits to store the magnitude of the number.

## Memory Layout and Bounds

The bounds of a `u16` are defined by associated constants in the standard library:

* `u16::MIN`: `0`
* `u16::MAX`: `65535`

## Syntax and Initialization

You can declare a `u16` using explicit type annotation, literal suffixes, or rely on the compiler's type inference. Rust also supports visual separators (`_`) and various radix representations (hexadecimal, octal, binary).

```rust theme={"dark"}
let dec_annotated: u16 = 65000;       // Explicit type annotation
let dec_suffixed = 65000_u16;         // Literal suffix
let hex_val = 0xFE00_u16;             // Hexadecimal (Base 16)
let oct_val = 0o177777_u16;           // Octal (Base 8)
let bin_val = 0b1111_0000_1111_u16;   // Binary (Base 2)
```

## Type Casting and Truncation

Conversions between `u16` and other primitive numeric types are performed using the `as` keyword.

When casting to a smaller type (e.g., `u8`), Rust truncates the higher-order bits. When casting to a larger type (e.g., `u32`), Rust performs zero-extension, filling the new higher-order bits with zeros.

```rust theme={"dark"}
let original: u16 = 0x1234; // 4660 in decimal

// Truncation: Keeps only the lowest 8 bits (0x34)
let downcast: u8 = original as u8; 

// Zero-extension: Pads the highest 16 bits with zeros (0x00001234)
let upcast: u32 = original as u32; 
```

## Overflow Behavior

Like all Rust integers, `u16` has strict overflow behavior determined by the compilation profile:

* **Debug mode (`dev`):** Integer operations that exceed `u16::MAX` or drop below `u16::MIN` will trigger a runtime panic.
* **Release mode (`release`):** Operations silently wrap around using two's complement wrapping (e.g., `65535 + 1` becomes `0`).

To explicitly control overflow behavior regardless of the compilation profile, `u16` provides dedicated method families:

```rust theme={"dark"}
let val: u16 = 65535;

// Wraps around to 0
let wrapped = val.wrapping_add(1); 

// Returns None on overflow
let checked = val.checked_add(1); 

// Caps at u16::MAX (65535) instead of overflowing
let saturated = val.saturating_add(1); 

// Returns a tuple: (0, true) indicating the result and that overflow occurred
let (result, overflowed) = val.overflowing_add(1); 
```

## Endianness and Byte Manipulation

Because `u16` is a multi-byte type, its memory representation is subject to endianness (byte order). The standard library provides methods to convert a `u16` to and from raw byte arrays (`[u8; 2]`) in specific byte orders.

```rust theme={"dark"}
let val: u16 = 0xABCD;

// Native endianness (depends on the target architecture)
let native_bytes: [u8; 2] = val.to_ne_bytes(); 

// Little-endian: Least significant byte first
let le_bytes: [u8; 2] = val.to_le_bytes(); // [0xCD, 0xAB]

// Big-endian (Network byte order): Most significant byte first
let be_bytes: [u8; 2] = val.to_be_bytes(); // [0xAB, 0xCD]

// Reconstructing from bytes
let reconstructed = u16::from_be_bytes([0xAB, 0xCD]);
```

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