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.

isize is a signed integer type in Rust whose bit width is statically determined at compile time by the pointer size of the target architecture. It occupies 16 bits on 16-bit systems, 32 bits on 32-bit systems, and 64 bits on 64-bit systems, ensuring its memory footprint exactly matches that of a machine pointer.

Memory Layout and Architecture Dependency

The size of isize is intrinsically linked to the target’s address space. It is the signed counterpart to usize and utilizes two’s complement representation for negative values. Because it is a Sized type, its dimensions are known at compile time and never evaluated dynamically at runtime.
// The size of isize is guaranteed to match the size of a raw pointer
assert_eq!(
    std::mem::size_of::<isize>(),
    std::mem::size_of::<*const ()>()
);

// Variable declaration
let offset: isize = -150;

Value Range

Because isize is architecture-dependent, its minimum and maximum bounds vary based on the compilation target. On 16-bit architectures (e.g., avr, msp430):
  • Minimum: -2^15 (-32,768)
  • Maximum: 2^15 - 1 (32,767)
On 32-bit architectures (e.g., i686, armv7):
  • Minimum: -2^31 (-2,147,483,648)
  • Maximum: 2^31 - 1 (2,147,483,647)
On 64-bit architectures (e.g., x86_64, aarch64):
  • Minimum: -2^63 (-9,223,372,036,854,775,808)
  • Maximum: 2^63 - 1 (9,223,372,036,854,775,807)
These boundaries are exposed as associated constants in the standard library:
let min_val: isize = isize::MIN;
let max_val: isize = isize::MAX;

Type Casting and Safety

Due to its variable bit width, casting between isize and fixed-width integer types (like i32 or i64) carries architecture-specific truncation or sign-extension semantics. Using the as keyword forces a cast. While casting primitive integers with as is completely safe and never results in undefined behavior (it has strictly defined two’s complement truncation or sign-extension semantics), it can cause logical bugs via silent data loss if the value exceeds the target type’s capacity:
let arch_dependent_val: isize = 42;

// Lossless on 16-bit and 32-bit architectures; 
// truncates the upper 32 bits on 64-bit architectures
let fixed_32: i32 = arch_dependent_val as i32;

// Lossless on 64-bit architectures; 
// sign-extends the value on 16-bit and 32-bit architectures
let fixed_64: i64 = arch_dependent_val as i64; 
To prevent silent data loss, Rust implements the TryFrom and TryInto traits for isize. These evaluate the runtime validity of the cast based on the compiled architecture and return a Result, ensuring that out-of-bounds conversions are explicitly handled rather than silently truncated.
use std::convert::TryInto;

let val: isize = isize::MAX;

// Returns Ok(val) on 16-bit and 32-bit architectures, 
// but returns Err(TryFromIntError) on 64-bit architectures 
// because 64-bit isize::MAX exceeds i32::MAX.
let safe_cast: Result<i32, _> = val.try_into(); 
Master Rust with Deep Grasping Methodology!Learn More