๐ Scenario: Imagine you are calculating the total cost of items in a shopping cart. You want to make sure the calculations happen in the right order so you get the correct total.
๐ฏ Goal: Build a Rust program that calculates the total cost using operator precedence rules correctly.
๐ What You'll Learn
Create variables with exact values
Use operator precedence in an expression
Print the final calculated total
๐ก Why This Matters
๐ Real World
Calculating prices in shopping carts or invoices requires correct operator precedence to avoid mistakes.
๐ผ Career
Understanding operator precedence is essential for writing correct calculations in software development, especially in finance and e-commerce.
Progress0 / 4 steps
1
Create item prices
Create three variables called apple_price, banana_price, and orange_price with values 2, 3, and 4 respectively.
Rust
Hint
Use let to create variables and assign the exact numbers.
2
Create quantity variables
Create three variables called apple_qty, banana_qty, and orange_qty with values 3, 2, and 5 respectively.
Rust
Hint
Use let to create quantity variables with the exact numbers.
3
Calculate total cost with operator precedence
Create a variable called total_cost that calculates the total price by multiplying each price by its quantity and adding all results together. Use the expression: apple_price * apple_qty + banana_price * banana_qty + orange_price * orange_qty.
Rust
Hint
Remember multiplication happens before addition without extra parentheses.
4
Print the total cost
Print the value of total_cost using println! with the exact text: Total cost: {total_cost} where {total_cost} is replaced by the variable value.
Rust
Hint
Use println!("Total cost: {}", total_cost); to print the result.
Practice
(1/5)
1. In Rust, which operator has the highest precedence among these: +, *, -, /?
easy
A. Multiplication (*) and division (/) have higher precedence than addition (+) and subtraction (-).
B. Addition (+) and subtraction (-) have higher precedence than multiplication (*) and division (/).
C. All operators have the same precedence and are evaluated left to right.
D. Subtraction (-) has the highest precedence among all.
Solution
Step 1: Recall Rust operator precedence rules
Multiplication (*) and division (/) operators have higher precedence than addition (+) and subtraction (-).
Step 2: Compare given operators
Among the given operators, * and / are evaluated before + and - in expressions.
Final Answer:
Multiplication (*) and division (/) have higher precedence than addition (+) and subtraction (-). -> Option A
Quick Check:
*, / > +, - [OK]
Hint: Remember: multiply/divide before add/subtract [OK]
Common Mistakes:
Thinking addition has higher precedence than multiplication
Assuming all operators have equal precedence
Confusing subtraction precedence with multiplication
2. Which of the following Rust expressions is correctly written to add 2 and 3, then multiply the result by 4?
easy
A. 2 + 3 * 4
B. (2 + 3) * 4
C. 2 + (3 * 4)
D. 2 * 3 + 4
Solution
Step 1: Understand operator precedence and parentheses
Multiplication (*) has higher precedence than addition (+), so without parentheses, multiplication happens first.
Step 2: Check each expression
(2 + 3) * 4 uses parentheses to add 2 and 3 first, then multiplies by 4, which matches the requirement.
Final Answer:
(2 + 3) * 4 -> Option B
Quick Check:
Parentheses force addition first [OK]
Hint: Use parentheses to change default order [OK]
Common Mistakes:
Ignoring parentheses and relying on default precedence
Misplacing parentheses causing wrong order
Assuming left-to-right always applies
3. What is the output of this Rust code?
let result = 10 - 4 / 2 + 3 * 2;
println!("{}", result);
medium
A. 13
B. 10
C. 11
D. 14
Solution
Step 1: Apply operator precedence to expression
Division and multiplication happen before addition and subtraction: 4 / 2 = 2 3 * 2 = 6