Start learning free

LESSON 19 OF 414 · CALCULATIONS WITH NUMBERS

Mathematical Expressions in Rust: Combining Operators

Lesson Read the full explanation

  • We can perform multiple calculations using operators in the same line of code:

    println!("{}", 3 - 4 + 6); // Prints 5
    println!("{}", 5 + 2 - 3); // Prints 4
    println!("{}", 2 * 5 + 1); // Prints 11
    println!("{}", 4 / 2 * 5); // Prints 10
    

  • We can also use variables:

    let num1: i32 = 5;
    let num2: i32 = -1;
    
    println!("{}", 2 * num1 * num2); // Prints -10
    println!("{}", num1 + num2 - 3); // Prints 1
    println!("{}", 20 / num1 + num2); // Prints 3
    println!("{}", num1 / 5 * num2); // Prints -1
    



Assignment
Follow the Coding Tutorial and let's practice with mathematical expressions!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 2
    Ctrl + Enter to check

    RUST · CALCULATIONS WITH NUMBERS

    Mathematical Expressions in Rust, explained

    What an expression is

    An expression is any piece of code that produces a value. 3 - 4 + 6 is an expression, and so is 2 * num1 * num2. You can chain as many operators as you like in one expression, using numbers, variables or both, and Rust evaluates the whole thing down to a single value that println! can print.

    println!("{}", 3 - 4 + 6); // 5
    println!("{}", 5 + 2 - 3); // 4
    println!("{}", 2 * 5 + 1); // 11
    println!("{}", 4 / 2 * 5); // 10
    

    Which operator goes first

    Two rules decide the order:

    1. *, / and % are evaluated before + and -. So 2 * 5 + 1 is 10 + 1.
    2. Operators at the same level run from left to right. So 3 - 4 + 6 is -1 + 6, and 4 / 2 * 5 is 2 * 5, which is 10.

    The second rule matters for subtraction and division. 10 - 4 - 3 is (10 - 4) - 3, which is 3, not 10 - (4 - 3), which is 9. Parentheses override the order whenever you need a different one.

    Using variables

    Variables take part in expressions exactly like numbers. Rust looks up each variable's value and then calculates:

    let num1: i32 = 5;
    let num2: i32 = -1;
    
    println!("{}", 2 * num1 * num2); // -10
    println!("{}", num1 + num2 - 3); // 1
    println!("{}", 20 / num1 + num2); // 3
    println!("{}", num1 / 5 * num2); // -1
    

    Take the third line: 20 / num1 is 20 / 5 = 4, then adding num2 (which is -1) gives 3. Since all the values are i32, every division is an integer division that drops the fraction. That affects order: with integers, 6 / 4 * 3 is 1 * 3 = 3, while 6 / (4 * 3) is 0. Mixing integers and floats in one expression is a compile error in Rust, so keep every operand the same type.

    Parentheses for readability

    Parentheses are also a tool for the reader. (2 * price) + bag - 150 evaluates exactly like 2 * price + bag - 150, but it makes the grouping obvious at a glance. When an expression gets long, a few redundant parentheses cost nothing and prevent misreading. A good habit is to work through an expression by hand, one operator at a time in the order above, and compare your answer with the output.

    The exercise, step by step

    The variables price and bag hold the cost of one product and one bag.

    Step 1: Andy's total

    Andy buys four products and one bag. The total is four times the price, plus the bag. Multiplication runs first, so 4 * price + bag needs no parentheses. With price equal to 93 and bag equal to 7, that is 372 + 7 = 379. Print it with println! and a {} placeholder.

    Step 2: what Mary still needs

    Mary has 150 dollars and wants two products and one bag. Her total is 2 * price + bag. Subtracting what she has gives 2 * price + bag - 150. The three operations run in this order: the multiplication, then the addition, then the subtraction from left to right, so the full total is built before 150 is taken away. With the same values, 186 + 7 = 193, and 193 - 150 leaves 43.

    The solution in Rust

    let price: i32 = 93;
    let bag: i32 = 7;
    
    println!("{}", 4 * price + bag);
    println!("{}", 2 * price + bag - 150);
    

    This prints 379 and then 43, each on its own line, because println! ends every call with a line break.

    Common mistakes

    • Adding before multiplying. 4 * (price + bag) is 400, not 379. It means four products each with a bag.
    • Misplaced parentheses. 2 * (price + bag - 150) subtracts 150 from the price of one product plus one bag and then doubles that, which prints -100 with the lesson's values. Parentheses change the meaning, so add them only when you want a different order.
    • Negative results. If Mary had more money than she needed, the same expression would print a negative number, meaning she has money left over.
    • Using 4 price or 4(price). Rust has no implied multiplication. Always write *.

    Testing your answer

    Change the values of price and bag and check the output by hand. With price equal to 10 and bag equal to 5, Andy pays 45, and the second expression gives 2 * 10 + 5 - 150 = -125: her purchase costs 25, so she has 125 dollars to spare and needs nothing more. A negative result is a quick sign that the second customer can already afford the purchase.

    Key takeaways

    • One expression can chain many operators, and the result is a single value.
    • *, / and % go before + and -, and equal-level operators go left to right.
    • Variables behave exactly like the numbers they hold.
    • With integers, division drops the fraction at the moment it runs, so the order can change the answer.
    Keep going with RustAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free