Code Example Template
let key_code: u32 = 0x41; // Windows Virtual Key for 'A'
let delta: i32 = -1;

Deep-Dive System Documentation

Standard 32-Bit Scalers

In Rust, i32 and u32 are the default integer primitives used for standard numeric operations, virtual key registrations in keyboard hooks, and index offsets.

Key Characteristics

  • i32: 32-bit signed integer. Ranges from $-2^{31}$ to $2^{31}-1$. This is the standard integer in Rust unless specified otherwise.
  • u32: 32-bit unsigned integer. Ranges from $0$ to $2^{32}-1$. Perfect for flags, colors, and Win32 virtual key mappings.

Practical Usage in System APIs

When writing FFI integrations (such as communicating with the Windows API), virtual keys and event codes are frequently handled as u32 types:

// A virtual key code representation in standard Windows API FFI
let key_code: u32 = 0x41; // Code for key 'A'

Performance Metrics

  • Storage Size: Exactly 4 bytes on the stack.
  • Access Cost: Instant register operations. Zero overhead.

Useful Methods

.abs() -> Self

Returns the absolute value of the integer. Only implemented on signed types like i32.

let val = -42i32;
assert_eq!(val.abs(), 42);

.pow(exp: u32) -> Self

Raises the integer to the power of exp.

let base = 2i32;
assert_eq!(base.pow(3), 8);

.saturating_add(rhs: Self) -> Self

Saturating integer addition. Computes self + rhs, saturating at the numeric bounds instead of overflowing/panicking.

let max = i32::MAX;
assert_eq!(max.saturating_add(1), max); // Saturates at boundary

.wrapping_add(rhs: Self) -> Self

Wrapping (modular) addition. Computes self + rhs, wrapping around the numeric bounds at boundary limits.

let max = u32::MAX;
assert_eq!(max.wrapping_add(1), 0); // Wraps back to zero

.checked_add(rhs: Self) -> Option<Self>

Checked integer addition. Computes self + rhs, returning None if overflow occurred, preventing system logic bugs.

let max = i32::MAX;
assert_eq!(max.checked_add(1), None); // Safely caught overflow

Quick Reference Guide

let signed: i32 = -42;
let unsigned: u32 = 100;