Arithmetic and integer division
Quick answer
Rust uses +, -, *, / and % for addition, subtraction, multiplication, division and remainder. Integer division discards the fractional part toward zero, while floating-point division can represent fractional results approximately.
Choose integer or fractional calculations
For a nonnegative item count, dividing by a group size gives full groups and the remainder gives leftover items. With signed integers, the remainder can be negative. Division of integers by zero is invalid; validate a divisor before using it. Numeric operands generally need compatible types.
fn main() { let items = 17; println!("Groups: {}, left: {}", items / 5, items % 5);}Expected stdout (not executed):
Groups: 3, left: 2Three complete groups use fifteen items, leaving two.
fn main() { println!("Integer: {}", -7 / 2); println!("Fraction: {:.1}", 7.0 / 2.0);}Expected stdout (not executed):
Integer: -3Fraction: 3.5The integer result is rounded toward zero, not toward negative infinity.
Common mistake
Do not calculate 7 / 2 expecting 3.5. Use floating-point operands for an approximate fraction.
Try it yourself
Pack 29 items into groups of four. Display full groups and leftovers.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Arithmetic and integer division — practice quiz
Choose one answer per question, then review the explanation.
What are quotient and remainder for 23 divided by 6 using integers?
What is `-9i32 % 4`?
What is the result of `9.0_f64 / 4.0`?