operator

Rust %: find the remainder

Use `%` to obtain the remainder after division. For integer operands a nonzero remainder has the sign of the dividend. Expected output (not executed): 35 -1 2 Why: Thirty-five minutes remain; Euclidean remainder provides a nonnegative result for this divisor. Common mistake: Do not assume `%` is always nonnegative. A zero integer divisor or signed MIN % -1 panics. Advanced connection: `std::ops::Rem`; `RemAssign` for `%=`.

Formal Syntax & Structure

rust syntaxrust
a % b

Working Example

Example Snippetrust
fn main() {    let minutes = 95;    println!("{}", minutes % 60);    println!("{}", -7_i32 % 3);    println!("{}", (-7_i32).rem_euclid(3));}
Rust Reference