Skip to main content
The i8 type in Rust is a primitive data type representing an 8-bit signed integer. It occupies exactly one byte of memory and encodes values using two’s complement binary representation, allowing it to represent both positive and negative whole numbers.

Technical Specifications

  • Memory Size: 8 bits (1 byte)
  • Minimum Value (i8::MIN): -128 (-27)
  • Maximum Value (i8::MAX): 127 (27 - 1)
  • Encoding: Two’s complement

Syntax and Initialization

You can declare an i8 using explicit type annotations or literal suffixes. Rust allows the use of underscores for visual separation, as well as hexadecimal, octal, and binary literal formats.

Overflow Behavior

Because i8 has a strict upper and lower bound, arithmetic operations that exceed these bounds result in integer overflow. Rust handles overflow differently depending on the compilation profile:
  • Debug mode: Integer overflow causes the program to panic.
  • Release mode: Integer overflow performs two’s complement wrapping (e.g., 127 + 1 becomes -128).
To explicitly control overflow behavior regardless of the compilation profile, Rust provides dedicated methods on the i8 type:

Type Conversion

Rust enforces explicit type conversion. The method of conversion depends on whether the operation is lossless, fallible, or lossy.
  • Lossless Promotion: Converting i8 to a wider signed integer (e.g., i16, i32) performs sign extension. Rust strongly encourages using the From and Into traits for these infallible conversions.
  • Fallible Conversion: Converting from a wider integer to i8 can fail if the value is out of bounds. This is handled safely using the TryFrom and TryInto traits, which return a Result.
  • Lossy Truncation and Sign Casting: The as keyword is strictly required when intentionally performing lossy conversions (truncating higher-order bits) or reinterpreting the exact bit pattern (casting between i8 and u8).
Tired of Poor Rust Skills? Fix That With Deep Grasping!Learn More