Skip to main content
u64 is a primitive unsigned integer type in Rust that occupies exactly 64 bits (8 bytes) of memory. It represents non-negative whole numbers strictly within the range of 0 to 18,446,744,073,709,551,615 (26412^{64} - 1). Because it is unsigned, it cannot represent negative values and does not allocate a sign bit.

Constants and Bounds

The standard library provides associated constants to access the boundaries of the type:

Syntax and Instantiation

Rust allows u64 initialization through explicit type annotation, literal suffixes, and various radix prefixes. Underscores can be inserted anywhere within numeric literals for visual grouping.

Type Casting

Casting to and from u64 is performed using the as keyword.
  • Casting from a smaller unsigned integer (u8, u16, u32) results in zero-extension.
  • Casting from a smaller signed integer (i8, i16, i32) results in sign-extension before interpreting the bits as unsigned.
  • Casting to a smaller integer type results in truncation of the most significant bits.

Integer Overflow Behavior

Rust handles u64 arithmetic overflows differently depending on the compilation profile:
  • Debug mode (dev): Integer operations that overflow will trigger a panic.
  • Release mode (release): Integer operations silently perform two’s complement wrapping.
To enforce specific overflow behaviors regardless of the compilation profile, u64 provides explicit arithmetic methods:

Memory Layout and Endianness

u64 is stored in memory as a contiguous 8-byte sequence. The standard library provides methods to manipulate and convert the byte order (endianness) of the integer, which is critical for network serialization or FFI.

Trait Implementations

As a primitive, u64 implements several core traits by default:
  • Copy and Clone: Passed by value rather than by reference.
  • Eq, PartialEq, Ord, PartialOrd: Fully comparable and sortable.
  • Hash: Can be used as keys in HashMap or HashSet.
  • Default: The default value is 0.
  • Bitwise traits (BitAnd, BitOr, BitXor, Shl, Shr): Supports standard bitwise operations.
Tired of Poor Rust Skills? Fix That With Deep Grasping!Learn More