std-method

Integer div_euclid and rem_euclid

For a positive divisor, use a nonnegative remainder to assign negative positions to cyclic buckets. Ordinary % can produce a negative remainder. Expected stdout (not executed): -2 1 -3 Common mistake: Assuming Euclidean division handles a zero divisor.

Formal Syntax & Structure

rust syntaxrust
x.div_euclid(y); x.rem_euclid(y)

Working Example

Example Snippetrust
fn main() {    let position = -7i32;    println!("{} {}", position.div_euclid(4), position.rem_euclid(4));    println!("{}", position % 4);}
Rust Reference