๐ Scenario: You are building a simple calculator program that evaluates basic arithmetic expressions given as strings. This is useful for understanding how computers process math expressions step-by-step.
๐ฏ Goal: Create a Rust program that stores an arithmetic expression as a string, sets a variable for a number to use in the expression, evaluates the expression using that variable, and prints the result.
๐ What You'll Learn
Create a string variable called expression with the value "3 + x * 2".
Create an integer variable called x and set it to 4.
Calculate the result of the expression 3 + x * 2 using the variable x.
Print the result using println!.
๐ก Why This Matters
๐ Real World
Calculators, spreadsheets, and many software tools need to evaluate math expressions entered by users.
๐ผ Career
Understanding expression evaluation is important for programming tasks involving math, data processing, and building user input features.
Progress0 / 4 steps
1
DATA SETUP: Create the expression string
Create a string variable called expression and set it to the exact value "3 + x * 2".
Rust
Hint
Use let expression = "3 + x * 2"; to create the string.
2
CONFIGURATION: Define the variable x
Create an integer variable called x and set it to 4.
Rust
Hint
Use let x = 4; to create the variable.
3
CORE LOGIC: Evaluate the expression using x
Calculate the result of the expression 3 + x * 2 using the variable x and store it in a variable called result.
Rust
Hint
Use let result = 3 + x * 2; to calculate the expression.
4
OUTPUT: Print the result
Print the value of the variable result using println!.
Rust
Hint
Use println!("{}", result); inside fn main() to print the result.
Practice
(1/5)
1. What does the following Rust expression do? let result = 5 + 3 * 2; Choose the correct description.
easy
A. Calculates 3 times 2, then adds 5
B. Multiplies 5 by 3, then adds 2
C. Calculates 5 plus 3, then multiplies by 2
D. Adds 5, 3, and 2 together
Solution
Step 1: Understand operator precedence in Rust
Multiplication (*) has higher precedence than addition (+), so 3 * 2 is calculated first.
Step 2: Calculate the expression step-by-step
3 * 2 = 6, then 5 + 6 = 11.
Final Answer:
Calculates 3 times 2, then adds 5 -> Option A
Quick Check:
5 + (3 * 2) = 11 [OK]
Hint: Remember multiplication before addition in expressions [OK]
Common Mistakes:
Adding numbers left to right ignoring precedence
Multiplying after addition
Confusing operator precedence
2. Which of the following is the correct way to assign the result of 4 + 7 to a variable named sum in Rust?
easy
A. let sum : 4 + 7;
B. sum = let 4 + 7;
C. let sum = 4 + 7;
D. let sum == 4 + 7;
Solution
Step 1: Recall Rust variable assignment syntax
In Rust, use let variable = expression; to assign a value.
Step 2: Check each option for correct syntax
let sum = 4 + 7; uses correct syntax: let sum = 4 + 7;. Others have syntax errors.
Final Answer:
let sum = 4 + 7; -> Option C
Quick Check:
Correct Rust assignment syntax [OK]
Hint: Use 'let var = expression;' to assign values [OK]
Common Mistakes:
Using '==' instead of '=' for assignment
Placing 'let' after variable name
Using ':' instead of '=' for assignment
3. What is the output of this Rust code?
fn main() {
let x = 10;
let y = 3;
let result = x / y + x % y;
println!("{}", result);
}