> ## 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.

# Rust i8

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 (-2<sup>7</sup>)
* **Maximum Value (`i8::MAX`):** 127 (2<sup>7</sup> - 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.

```rust theme={"dark"}
// Explicit type annotation
let decimal: i8 = 42;
let negative: i8 = -128;

// Literal suffix
let suffixed = 100i8;

// Alternative base literals
let hex: i8 = 0x7F;        // 127 in hexadecimal
let oct: i8 = 0o177;       // 127 in octal
let bin: i8 = 0b0111_1111; // 127 in binary
```

## 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:

```rust theme={"dark"}
let max: i8 = 127;

// Explicit wrapping (returns -128)
let wrapped = max.wrapping_add(1);

// Checked addition (returns Option::None on overflow)
let checked = max.checked_add(1);

// Saturating addition (clamps to i8::MAX, returns 127)
let saturated = max.saturating_add(1);

// Overflowing addition (returns a tuple: (-128, true))
let (result, did_overflow) = max.overflowing_add(1);
```

## 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`).

```rust theme={"dark"}
let val: i8 = -5;

// Lossless promotion using Into/From (Sign extension)
let wider: i32 = val.into(); 
let wider_explicit = i32::from(val);

// Fallible conversion using TryInto (Returns Result)
let large_val: i32 = 300;
let checked_narrow: Result<i8, _> = large_val.try_into(); // Err(TryFromIntError)

// Lossy truncation using `as`
let truncated: i8 = large_val as i8; // 44 (keeps only the lowest 8 bits)

// Bit-pattern reinterpretation using `as` (Sign casting)
let unsigned: u8 = val as u8; // 251 (0b1111_1011)
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Rust Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
