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.

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 (21612^{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).
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.
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:
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.
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]);
Master Rust with Deep Grasping Methodology!Learn More