0
0
Rustprogramming~10 mins

Relational operators in Rust - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Relational operators
Start
Evaluate Left Operand
Evaluate Right Operand
Compare using relational operator
Result: true or false
Use result in program
End
Relational operators compare two values and return true or false based on the comparison.
Execution Sample
Rust
fn main() {
    let a = 5;
    let b = 3;
    let result = a > b;
    println!("{} > {} is {}", a, b, result);
}
This code compares two numbers using the greater than operator and prints the result.
Execution Table
StepActionEvaluationResult
1Assign a = 5a = 5a = 5
2Assign b = 3b = 3b = 3
3Compare a > b5 > 3true
4Assign result = trueresult = trueresult = true
5Print result"5 > 3 is true"Output: 5 > 3 is true
💡 Program ends after printing the comparison result.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4Final
auninitialized5555
buninitializeduninitialized333
resultuninitializeduninitializeduninitializedtruetrue
Key Moments - 3 Insights
Why does the comparison a > b return true even though both are numbers?
Because 5 is greater than 3, the relational operator '>' returns true as shown in step 3 of the execution_table.
What type of value does a relational operator produce?
It produces a boolean value (true or false), as seen in step 3 and stored in 'result' in step 4.
Can relational operators be used with types other than numbers?
Yes, but in this example, we only compare numbers. The operator compares values based on their type's rules.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'result' after step 4?
Afalse
Btrue
C5
D3
💡 Hint
Check the 'Result' column in row for step 4 in execution_table.
At which step does the program print the output?
AStep 3
BStep 4
CStep 5
DStep 2
💡 Hint
Look for the step with action 'Print result' in execution_table.
If we change b to 7, what would be the result of 'a > b'?
Afalse
Btrue
C5
D7
💡 Hint
Compare 5 > 7 logically and relate to step 3 in execution_table.
Concept Snapshot
Relational operators compare two values and return true or false.
Common operators: >, <, >=, <=, ==, !=
Used to check conditions in code.
Result is always a boolean.
Example: let result = a > b;
Print with println! macro.
Full Transcript
This example shows how relational operators work in Rust. We start by assigning values to variables a and b. Then we compare a > b using the greater than operator. This comparison returns a boolean true or false. The result is stored in the variable 'result'. Finally, we print the result using println!. The execution table shows each step clearly, including variable assignments, comparison, and output. Beginners often wonder why the comparison returns true or what type of value it produces. The answer is that relational operators always return a boolean value based on the comparison. Changing the values changes the comparison result accordingly.