Expression evaluation means calculating the result of a math or logic statement in code. It helps the computer decide what value to use next.
Expression evaluation in Rust
let result = expression;
An expression can be a math operation like 5 + 3 or a comparison like 4 > 2.
The result of the expression is stored in a variable using let.
sum.let sum = 5 + 3;
true in is_greater.let is_greater = 10 > 7;
combined.let combined = (4 + 2) * 3;
This program calculates and prints the sum, difference, product, and quotient of two numbers. It also checks if one number is greater than the other.
fn main() {
let a = 10;
let b = 5;
let sum = a + b;
let difference = a - b;
let product = a * b;
let quotient = a / b;
let is_a_greater = a > b;
println!("Sum: {}", sum);
println!("Difference: {}", difference);
println!("Product: {}", product);
println!("Quotient: {}", quotient);
println!("Is a greater than b? {}", is_a_greater);
}Rust follows the usual math order: multiplication and division happen before addition and subtraction.
Division between integers gives an integer result (no decimals).
Boolean expressions like comparisons return true or false.
Expressions calculate values using math or logic.
Use let to save the result of an expression in a variable.
Expressions can be combined and used to make decisions or print results.
Practice
let result = 5 + 3 * 2;Choose the correct description.
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 AQuick Check:
5 + (3 * 2) = 11 [OK]
- Adding numbers left to right ignoring precedence
- Multiplying after addition
- Confusing operator precedence
4 + 7 to a variable named sum in Rust?Solution
Step 1: Recall Rust variable assignment syntax
In Rust, uselet 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 CQuick Check:
Correct Rust assignment syntax [OK]
- Using '==' instead of '=' for assignment
- Placing 'let' after variable name
- Using ':' instead of '=' for assignment
fn main() {
let x = 10;
let y = 3;
let result = x / y + x % y;
println!("{}", result);
}Solution
Step 1: Calculate integer division and remainder
10 / 3 = 3 (integer division), 10 % 3 = 1 (remainder).Step 2: Add the results
3 + 1 = 4.Final Answer:
4 -> Option DQuick Check:
10 / 3 + 10 % 3 = 4 [OK]
- Treating division as float division
- Confusing remainder with division result
- Adding operands incorrectly
fn main() {
let a = 5;
let b = 0;
let c = a / b;
println!("{}", c);
}Solution
Step 1: Analyze the division operation
The code divides 'a' by 'b', where 'b' is zero.Step 2: Understand Rust runtime behavior
Dividing by zero in Rust causes a runtime panic (crash), not a compile error.Final Answer:
Division by zero causes a runtime panic -> Option AQuick Check:
Division by zero panics in Rust [OK]
- Thinking division by zero is a compile error
- Ignoring runtime panics
- Assuming variable declaration errors
let numbers = [1, 2, 3, 4, 5, 6];
let sum_even = /* fill here */;
println!("{}", sum_even);Choose the correct expression to replace
/* fill here */.Solution
Step 1: Understand the goal
We want to sum only even numbers from the list.Step 2: Analyze each option
numbers.iter().filter(|&&x| x % 2 == 0).sum::<i32>() filters even numbers (x % 2 == 0) and sums them. numbers.iter().map(|x| x * 2).sum::<i32>() doubles all numbers then sums (wrong). numbers.iter().filter(|&x| x % 2 != 0).sum::<i32>() sums odd numbers (wrong). numbers.iter().sum::<i32>() sums all numbers (wrong).Final Answer:
numbers.iter().filter(|&&x| x % 2 == 0).sum::<i32>() -> Option BQuick Check:
Filter evens then sum [OK]
- Summing all numbers without filtering
- Filtering odd numbers instead of even
- Mapping values incorrectly before sum
Ready to prove it?
Pass the test and this pattern moves unknown
