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.

i64 is a primitive signed integer type in Rust that occupies 64 bits (8 bytes) of memory. It represents whole numbers using two’s complement binary encoding, utilizing the most significant bit (MSB) as the sign bit to distinguish between positive and negative values.

Technical Specifications

  • Memory Size: 64 bits (8 bytes)
  • Encoding: Two’s complement
  • Minimum Value (i64::MIN): 263-2^{63} (-9,223,372,036,854,775,808)
  • Maximum Value (i64::MAX): 26312^{63} - 1 (9,223,372,036,854,775,807)

Syntax and Instantiation

Rust provides multiple ways to declare an i64 value, including explicit type annotations and literal suffixes. Visual separators (_) can be used to improve literal readability without affecting the compiled value.
// Explicit type annotation
let a: i64 = 100_000;

// Literal suffix
let b = -50i64;
let c = 1_000_000_i64;

// Base-specific literals
let hex: i64 = 0x1A_i64;       // Hexadecimal
let octal: i64 = 0o77_i64;     // Octal
let binary: i64 = 0b1010_i64;  // Binary

Overflow and Underflow Handling

By default, Rust handles i64 arithmetic overflows differently based on the compilation profile:
  • Debug profile: Integer overflow triggers a thread panic.
  • Release profile: Integer overflow performs two’s complement wrapping.
To explicitly dictate overflow behavior regardless of the compilation profile, i64 provides dedicated standard library methods:
let max = i64::MAX;

// Returns an Option<i64>; evaluates to None if overflow occurs
let checked = max.checked_add(1); 

// Wraps around the boundary (evaluates to i64::MIN)
let wrapped = max.wrapping_add(1); 

// Caps at the boundary (evaluates to i64::MAX)
let saturated = max.saturating_add(1); 

// Returns a tuple (i64, bool) containing the wrapped result and an overflow flag
let (overflowed_val, did_overflow) = max.overflowing_add(1); 

Type Casting

Conversion between i64 and other primitive numeric types is performed using the as keyword. Casting a larger integer type to i64 (e.g., i128 as i64) truncates the higher-order bits, while casting a smaller type (e.g., i32 as i64) performs sign extension to preserve the numeric value.
let small_int: i32 = -42;
let extended: i64 = small_int as i64; // Sign-extended to 64 bits

let large_int: i128 = 10_000_000_000_000_000_000_000;
let truncated: i64 = large_int as i64; // Higher-order bits are truncated

let float_val: f64 = 99.99;
let from_float: i64 = float_val as i64; // Fractional part is truncated (evaluates to 99)

Trait Implementations

As a core primitive, i64 implements several fundamental traits, ensuring integration with Rust’s strict type system and standard library operations:
  • Memory/Concurrency: Copy, Clone, Send, Sync
  • Comparison: Eq, PartialEq, Ord, PartialOrd
  • Arithmetic: Add, Sub, Mul, Div, Rem, Neg (and their Assign variants)
  • Bitwise: BitAnd, BitOr, BitXor, Shl, Shr (and their Assign variants)
  • Formatting: Display, Debug, LowerHex, UpperHex, Octal, Binary
Master Rust with Deep Grasping Methodology!Learn More