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

`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 ($2^{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:

```rust theme={"dark"}
let min_val: u64 = u64::MIN; // 0
let max_val: u64 = u64::MAX; // 18446744073709551615
```

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

```rust theme={"dark"}
// Explicit type annotation
let decimal: u64 = 1_000_000;

// Literal suffix (type inference)
let suffixed = 42u64;

// Radix prefixes
let hex: u64 = 0xDEAD_BEEF_0123_4567; // Base 16
let octal: u64 = 0o777_644;           // Base 8
let binary: u64 = 0b1010_1100_0011;   // Base 2

// Byte literal cast
let from_byte = b'A' as u64;          // 65
```

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

```rust theme={"dark"}
let small: u32 = 42;
let widened: u64 = small as u64; // Zero-extended to 64 bits

let large: u64 = 0x1122_3344_5566_7788;
let truncated: u32 = large as u32; // 0x5566_7788 (Higher 32 bits discarded)
```

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

```rust theme={"dark"}
let val = u64::MAX;

// Checked: Returns Option::None on overflow
let checked: Option<u64> = val.checked_add(1); 

// Wrapping: Wraps around using two's complement (returns 0)
let wrapped: u64 = val.wrapping_add(1); 

// Saturating: Clamps to the numeric bound (returns u64::MAX)
let saturated: u64 = val.saturating_add(1); 

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

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

```rust theme={"dark"}
let num: u64 = 0x1234_5678_90AB_CDEF;

// Convert to a byte array in Big-Endian (network byte order)
let be_bytes: [u8; 8] = num.to_be_bytes(); 
// [0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF]

// Convert to a byte array in Little-Endian
let le_bytes: [u8; 8] = num.to_le_bytes(); 
// [0xEF, 0xCD, 0xAB, 0x90, 0x78, 0x56, 0x34, 0x12]

// Reconstruct from bytes
let reconstructed = u64::from_be_bytes(be_bytes);
```

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

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