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:
*,/and%are evaluated before+and-. So2 * 5 + 1is10 + 1.- Operators at the same level run from left to right. So
3 - 4 + 6is-1 + 6, and4 / 2 * 5is2 * 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 priceor4(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.
