Start learning free

LESSON 26 OF 414 · CALCULATIONS WITH NUMBERS

Modulo Operator in Rust: Remainders with %

Lesson Read the full explanation

TL ; DR:

  • The modulo operator (%) calculates the remainder of dividing two values:

    println!("{}", 10 % 2); // Output: 0
    println!("{}", 15 % 4); // Output: 3
    println!("{}", 20 % 3); // Output: 2
    

  • It can also be used with variables:

    let a: i32 = 2;
    let b: i32 = 4;
    
    println!("{}", b % a); // Output: 0
    println!("{}", 11 % b); // Output: 3
    


Full lesson:

Remember how we first learn about the division of two integer numbers in primary school?

The quotient is the number of times a division is completed fully, while the remainder is the amount left that doesn't entirely go into the divisor.

Here are some examples:

10 / 2 = quotient 5, remainder 0
15 / 4 = quotient 3, remainder 3
20 / 3 = quotient 6, remainder 2

The modulo operator:

The modulo operator (%) calculates the remainder of dividing two values:

println!("{}", 10 % 2); // Output: 0
println!("{}", 15 % 4); // Output: 3
println!("{}", 20 % 3); // Output: 2

It can also be used with variables:

let a: i32 = 2;
let b: i32 = 4;

println!("{}", b % a); // Output: 0
println!("{}", 11 % b); // Output: 3

Integral Division:

Remember that in Rust, division between integers with / returns only the integral part of the result:

println!("{}", 10 / 2); // Output: 5
println!("{}", 15 / 4); // Output: 3
println!("{}", 20 / 3); // Output: 6

Quotient and remainder:

In programming, we combine both these concepts to get the quotient and remainder of some divison:

// Let's divide 26 by 3:
let quotient: i32 = 26 / 3;
let remainder: i32 = 26 % 3;

println!("{}", quotient); // Output: 8
println!("{}", remainder); // Output: 2

Assignment
Follow the Coding Tutorial and let's practice with quotient and remainder!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    RUST · CALCULATIONS WITH NUMBERS

    Modulo Operator in Rust, explained

    What the modulo operator does

    When you divide one whole number by another, the result has two parts. The quotient is how many times the divisor fits completely. The remainder is what is left over. For 20 divided by 3, the divisor fits 6 times (6 × 3 = 18), and 2 is left over.

    In Rust, the / operator on two integers gives the quotient and the % operator gives the remainder. The % operator is called the modulo or remainder operator.

    println!("{}", 10 % 2); // 0
    println!("{}", 15 % 4); // 3
    println!("{}", 20 % 3); // 2
    

    It works with variables as well as literal numbers:

    let a: i32 = 2;
    let b: i32 = 4;
    
    println!("{}", b % a); // 0
    println!("{}", 11 % b); // 3
    

    Quotient and remainder together

    Integer division in Rust drops the fractional part, so 15 / 4 is 3, not 3.75. The remainder is the part that was dropped, expressed as a whole number. The two operators always fit together: quotient * divisor + remainder gives back the original number.

    let quotient: i32 = 26 / 3;
    let remainder: i32 = 26 % 3;
    
    println!("{}", quotient);  // 8
    println!("{}", remainder); // 2
    

    Check: 8 × 3 + 2 = 26.

    Where % is useful

    • Leftovers. If you have 70 items and each pack holds 15, then 70 / 15 is 4 full packs and 70 % 15 is the 10 items that do not fill a pack.
    • Even or odd. A number n is even when n % 2 is 0 and odd when it is 1.
    • Divisibility. n % k == 0 means k divides n exactly.
    • Wrapping around. Adding to a position and taking the remainder by the length of a cycle keeps the result in range. If today is day 5 of a 7-day week, then (5 + 4) % 7 is 2.

    Rust details to know

    Negative numbers

    For integers, the sign of the result follows the left operand (the dividend):

    println!("{}", -7 % 3); // -1
    println!("{}", 7 % -3); // 1
    

    If you want a result that is never negative, use the method rem_euclid. With an i32 value, (-7i32).rem_euclid(3) is 2.

    Floating-point numbers

    The % operator also works on f64 and f32. For example, 7.5 % 2.0 is 1.5. Both operands must have the same type: Rust does not mix an integer and a float in one expression.

    Dividing by zero

    If the right-hand side of an integer % is zero, the program panics with the message "attempt to calculate the remainder with a divisor of zero". When the divisor is a plain literal 0, the compiler usually catches the mistake before the program runs. When it comes from a variable or user input, check it first.

    Augmented form

    Like the other arithmetic operators, % has a combined form: x %= 3 means x = x % 3, and it needs a variable declared with let mut.

    The exercise

    The starter code gives Andy some money and a price per product:

    let amount: i32 = 70;
    let price: i32 = 15;
    

    Andy buys as many products as he can afford, and the exercise asks for the money left over. Money that does not divide evenly by the price is exactly the remainder, so the whole solution is one line that prints amount % price:

    let amount: i32 = 70;
    let price: i32 = 15;
    
    println!("{}", amount % price);
    

    Here 15 fits into 70 four times (4 × 15 = 60), so 10 is left, and the program prints 10. Two details matter: println! needs the format string "{}" as its first argument, and the expression to print follows after a comma. Writing println!(amount % price) does not compile.

    Common mistakes

    • Using / when you want the leftovers. 70 / 15 is 4, the number of products. 70 % 15 is 10, the money left.
    • Swapping the operands. price % amount is 15 % 70, which is 15, because 70 does not fit into 15 even once. Whenever the left number is smaller than the right one, the remainder is the left number itself.
    • Treating % as a percentage. It has nothing to do with percentages.
    • Expecting a non-negative result for negative inputs. Use rem_euclid when you need that.

    Key takeaways

    • a % b is the remainder of dividing a by b; a / b on integers is the quotient.
    • quotient * divisor + remainder equals the original number.
    • For integers, the result takes the sign of the left operand; rem_euclid never returns a negative number for a positive divisor.
    • An integer remainder by zero panics.
    Keep going with RustAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free