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.

i32 is a primitive, fixed-size data type in Rust representing a 32-bit signed integer. It occupies exactly 4 bytes of memory and encodes values using two’s complement representation, allowing it to store both positive and negative whole numbers.

Technical Specifications

  • Memory Size: 32 bits (4 bytes)
  • Minimum Value (i32::MIN): -2,147,483,648 (231-2^{31})
  • Maximum Value (i32::MAX): 2,147,483,647 (23112^{31} - 1)
  • Type Inference: The Rust compiler (rustc) defaults to i32 for any unannotated integer literal unless constrained by the surrounding type context.

Syntax and Instantiation

Values of type i32 can be declared using explicit type annotation, literal suffixes, or implicit type inference.
// Explicit type annotation
let explicit_int: i32 = 42;

// Literal type suffix
let suffixed_int = 42_i32;

// Implicit inference (defaults to i32)
let inferred_int = 42; 

// Visual separators (underscores are ignored by the compiler)
let large_int: i32 = 1_000_000;
i32 supports standard numeric literal prefixes for different bases:
let hex_val: i32 = 0x2A;        // Hexadecimal (42 in decimal)
let octal_val: i32 = 0o52;      // Octal (42 in decimal)
let binary_val: i32 = 0b101010; // Binary (42 in decimal)

Overflow Behavior

Rust handles i32 arithmetic overflow differently depending on the compilation profile:
  • Debug mode (cargo build): Integer operations that exceed the i32 bounds trigger a runtime panic.
  • Release mode (cargo build --release): The compiler performs standard two’s complement wrapping. Values wrap through the boundary rather than stopping at it (e.g., i32::MAX + 1 results in i32::MIN, and i32::MAX + 2 results in i32::MIN + 1), without panicking.
To explicitly control overflow behavior regardless of the build profile, the i32 primitive provides standard library methods:
let max = i32::MAX;

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

// Wraps through the boundary to i32::MIN (-2147483648)
let wrapped = max.wrapping_add(1); 

// Caps at the boundary, remaining at i32::MAX (2147483647)
let saturated = max.saturating_add(1); 

// Returns a tuple (i32, bool) indicating the wrapped value and an overflow flag
let (overflowing, did_overflow) = max.overflowing_add(1);

Memory Layout and Casting

i32 implements the Copy trait, meaning reassignments perform a bitwise duplication of the value rather than moving ownership. The memory location of an i32 is determined by its allocation context: it is stored on the stack when declared as a local variable, but resides on the heap when owned by a heap-allocated collection or smart pointer (e.g., Vec<i32> or Box<i32>). Casting i32 to other numeric types requires the explicit as keyword. Casting to a smaller integer type (e.g., i16 or i8) truncates the higher-order bits.
let base_val: i32 = 300;

// Lossless cast to a larger type
let larger_val: i64 = base_val as i64; 

// Truncating cast to a smaller type (300 becomes 44 in i8)
let truncated_val: i8 = base_val as i8; 
Master Rust with Deep Grasping Methodology!Learn More