0
0
Rustprogramming~15 mins

Operator precedence in Rust - Mini Project: Build & Apply

Choose your learning style9 modes available
Operator precedence
๐Ÿ“– 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use println!("Total cost: {}", total_cost); to print the result.