%Operator
Explanation
% is the remainder operator, overloadable via std::ops::Rem.
It's the remainder, not strictly modulo — for negative operands, the
result takes the sign of the dividend (-7 % 2 == -1, not 1), which
differs from the mathematical modulo used by some other languages. Like
/, % panics on division by zero for integers.
Usage examples
Computing a remainder
let r = 7 % 2; // <- `%` computes the remainder
Restriction: dividing (or taking the remainder) by zero panics unconditionally for integers, even in release builds.
Numeric computation
Wrapping a ring buffer index into range is one of %'s most common
jobs — (idx + 1) % len computes the next slot without ever needing a
branch to reset back to zero.
struct RingBuffer {
slots: [u8; 4],
next: usize,
}
impl RingBuffer {
fn advance(&mut self) {
self.next = (self.next + 1) % self.slots.len(); // <- `%` wraps the index
}
}
let mut buf = RingBuffer { slots: [0; 4], next: 3 };
buf.advance();
assert_eq!(buf.next, 0); // wrapped past the end back to slot 0
% len only wraps correctly because len is never
zero for a fixed-size array — for a length that could legitimately be
zero, prefer checked_rem over risking the panic this page's
Explanation calls out, per the
standard library docs.