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

The `%` operator in Rust computes the remainder of a division operation. Strictly defined, it is a truncating remainder operator rather than a mathematical modulo operator, meaning the sign of the result always matches the sign of the dividend (the left-hand operand).

Under the hood, the `%` operator is syntactic sugar for the `std::ops::Rem` trait.

```rust theme={"dark"}
pub trait Rem<Rhs = Self> {
    type Output;
    fn rem(self, rhs: Rhs) -> Self::Output;
}
```

## Integer Mechanics and Signage

For integer types, the operation satisfies the equation `(a / b) * b + (a % b) == a`. Because Rust's integer division (`/`) truncates towards zero, the remainder must carry the sign of the left operand to satisfy this equation.

```rust theme={"dark"}
let a =  10 %  3; //  1
let b = -10 %  3; // -1
let c =  10 % -3; //  1
let d = -10 % -3; // -1
```

If you require a mathematical modulo where the result is strictly non-negative, you must use the `rem_euclid` method instead of the `%` operator. This method computes the Euclidean remainder, which guarantees the result is always non-negative (`0 <= r < |divisor|`), regardless of whether the divisor is positive or negative.

```rust theme={"dark"}
let r1 = (-10_i32).rem_euclid(3);  // 2
let r2 = (-10_i32).rem_euclid(-3); // 2
```

## Floating-Point Mechanics

The `%` operator is natively implemented for floating-point primitives (`f32` and `f64`). It computes the floating-point remainder matching the same truncating logic used for integers.

In accordance with IEEE 754 standards, floating-point remainder by zero does not trigger a panic. Instead, evaluating `x % 0.0` results in `NaN` (Not a Number).

```rust theme={"dark"}
let f1 = 5.5_f64 % 2.0_f64;  //  1.5
let f2 = -5.5_f64 % 2.0_f64; // -1.5
let f3 = 5.5_f64 % 0.0_f64;  //  NaN
```

## Panic and Overflow Conditions

The `%` operator exhibits specific behaviors for invalid integer operations:

1. **Integer Division by Zero:** Evaluating `x % 0` for any integer type triggers a runtime panic unconditionally, in both debug and release profiles.
2. **Signed Integer Overflow:** Evaluating `T::MIN % -1` for any signed integer type `T` (e.g., `i32::MIN % -1`) triggers a runtime panic unconditionally in all compilation profiles, yielding an "attempt to calculate the remainder with overflow" error. This occurs because the underlying hardware division instructions (such as `idiv` on x86 architectures) compute both the quotient and remainder simultaneously, and the quotient `T::MIN / -1` overflows the capacity of two's complement representation. To safely compute this remainder without panicking, developers must use the `overflowing_rem` method.

## Compound Assignment

Rust provides the `%=` operator for in-place remainder assignment. This mutates the left-hand operand and is backed by the `std::ops::RemAssign` trait.

```rust theme={"dark"}
let mut x = 10;
x %= 3; // x is now 1
```

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