0
0
Rustprogramming~10 mins

Logical operators in Rust - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Logical operators
Start
Evaluate Left Operand
Evaluate Right Operand
Apply Logical Operator (&&, ||, !)
Result: true or false
Use Result in Condition or Expression
End
Logical operators combine or invert boolean values to produce true or false results used in decisions.
Execution Sample
Rust
fn main() {
    let a = true;
    let b = false;
    let c = a && b;
    let d = a || b;
    let e = !a;
    println!("c: {}, d: {}, e: {}", c, d, e);
}
This code shows how &&, ||, and ! work with true and false values.
Execution Table
StepExpressionEvaluationResult
1a = trueAssign true to aa = true
2b = falseAssign false to bb = false
3c = a && btrue && falsefalse
4d = a || btrue || falsetrue
5e = !a!truefalse
6println!("c: {}, d: {}, e: {}", c, d, e)Print valuesc: false, d: true, e: false
💡 All expressions evaluated and printed; program ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5Final
auninitializedtruetruetruetruetruetrue
buninitializeduninitializedfalsefalsefalsefalsefalse
cuninitializeduninitializeduninitializedfalsefalsefalsefalse
duninitializeduninitializeduninitializeduninitializedtruetruetrue
euninitializeduninitializeduninitializeduninitializeduninitializedfalsefalse
Key Moments - 3 Insights
Why does 'a && b' result in false even though 'a' is true?
Because '&&' means both must be true. Since 'b' is false (see step 3 in execution_table), the whole expression is false.
What does the '!' operator do to 'a' in '!a'?
'!' flips the value. Since 'a' is true, '!a' becomes false (see step 5 in execution_table).
Why is 'a || b' true even though 'b' is false?
'||' means either one is true. Since 'a' is true, the whole expression is true (see step 4 in execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of 'c'?
Atrue
Bundefined
Cfalse
Dtrue && false
💡 Hint
Check the 'Result' column at step 3 in execution_table.
At which step does the variable 'e' get its value?
AStep 3
BStep 5
CStep 2
DStep 6
💡 Hint
Look for '!a' evaluation in execution_table.
If 'b' was true instead of false, what would be the value of 'c' at step 3?
Atrue
Bfalse
Cfalse && true
Dundefined
💡 Hint
Recall that '&&' is true only if both sides are true (see step 3).
Concept Snapshot
Logical operators in Rust:
- && (AND): true if both true
- || (OR): true if one true
- ! (NOT): flips true/false
Used to combine or invert boolean values in conditions.
Full Transcript
This visual trace shows how Rust evaluates logical operators. Variables a and b are assigned true and false. Then c is a && b, which is false because b is false. d is a || b, true because a is true. e is !a, which flips true to false. The program prints these results. Logical operators combine or invert boolean values to decide true or false outcomes.