Turn a question into an expression
How many rows of five can you fill with 23 items? Give the two counts names with let. Since both values are integers, items / row yields the number of whole rows. The remainder operator, %, gives the three items still on the table.
Choose whole or fractional units
The literal 23 has type i64, a signed 64-bit integer. The literal 23.0 has type f64, a 64-bit floating-point number. The expression 23.0 / 5.0 yields 4.6. Use integer counts for indivisible objects and decimal operands on both sides when a fraction matters.
Group an expression
Multiplication and division bind more tightly than addition and subtraction. Read 2 + 3 * 4 as 2 + (3 * 4), which gives 14. In the editor, parentheses make (2 + 3) happen first, giving 20.
Choose useful test cases
A full row, one fewer than a full row, and zero items each tell you something different about the result. With a nonzero row size, full_rows * row + leftover reconstructs the input. Integer division by zero and integer overflow produce execution errors.
Try the program
Expected output
4 3 4.6 20
Practice
Pack 24 items in rows of five. Predict both integer results before running.
Hint
The division and remainder use items. The separate decimal example uses literal values.
Worked solution
Four complete rows hold 20 items. Four items remain. The decimal expression and the grouped expression still use their original literals.
Find
let items = 23;Replace with
let items = 24;Output after the change
4 4 4.6 20