Bird
Raised Fist0
Rustprogramming~10 mins

Logical operators in Rust - Step-by-Step Execution

Choose your learning style10 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.

Practice

(1/5)
1. Which logical operator in Rust means AND and returns true only if both conditions are true?
easy
A. !
B. ||
C. &
D. &&

Solution

  1. Step 1: Understand the meaning of logical AND

    The AND operator returns true only when both conditions it connects are true.
  2. Step 2: Identify Rust's AND operator

    In Rust, the AND operator is written as &&.
  3. Final Answer:

    && -> Option D
  4. Quick Check:

    AND = && [OK]
Hint: AND operator is double ampersand && in Rust [OK]
Common Mistakes:
  • Confusing || (OR) with && (AND)
  • Using single & which is bitwise AND
  • Mistaking ! for AND
2. Which of the following is the correct syntax to negate a boolean variable flag in Rust?
easy
A. not flag
B. !flag
C. flag!
D. ~flag

Solution

  1. Step 1: Recall Rust's NOT operator syntax

    Rust uses the exclamation mark ! before a boolean to negate it.
  2. Step 2: Apply negation to variable flag

    Writing !flag returns the opposite boolean value of flag.
  3. Final Answer:

    !flag -> Option B
  4. Quick Check:

    NOT = ! [OK]
Hint: Negate booleans with ! before the variable [OK]
Common Mistakes:
  • Writing negation after the variable (flag!)
  • Using 'not' keyword which Rust does not support
  • Using ~ which is bitwise NOT, not boolean NOT
3. What is the output of this Rust code?
let a = true;
let b = false;
println!("{}", a || b && !a);
medium
A. Compilation error
B. false
C. true
D. Syntax error

Solution

  1. Step 1: Evaluate the expression with operator precedence

    AND (&&) has higher precedence than OR (||), so evaluate b && !a first.
    Here, b = false, a = true, so !a = false.
    Thus, b && !a = false && false = false.
  2. Step 2: Evaluate the OR operation

    Now evaluate a || (result) which is true || false.
    This results in true.
  3. Final Answer:

    true -> Option C
  4. Quick Check:

    Operator precedence: && before || = true [OK]
Hint: Remember && before || in expressions [OK]
Common Mistakes:
  • Evaluating || before &&
  • Ignoring negation operator precedence
  • Assuming false because b is false
4. Identify the error in this Rust code snippet:
let x = true;
let y = false;
if x & y {
println!("Both true");
}
medium
A. Using & instead of && for logical AND
B. Missing semicolon after println!
C. Incorrect variable names
D. No error, code runs fine

Solution

  1. Step 1: Check the operator used in the if condition

    The code uses & which is a bitwise AND operator, not logical AND.
  2. Step 2: Identify correct logical AND operator in Rust

    Logical AND requires && to combine boolean values properly.
  3. Final Answer:

    Using & instead of && for logical AND -> Option A
  4. Quick Check:

    Logical AND needs &&, not & [OK]
Hint: Use && for logical AND, & is bitwise AND [OK]
Common Mistakes:
  • Confusing bitwise & with logical &&
  • Assuming & works for booleans
  • Ignoring compiler warnings about operator types
5. You want to check if a number n is between 10 and 20 inclusive in Rust. Which condition correctly uses logical operators?
hard
A. n >= 10 && n <= 20
B. n >= 10 || n <= 20
C. !(n < 10 && n > 20)
D. n > 10 && n < 20

Solution

  1. Step 1: Understand the range condition

    To check if n is between 10 and 20 inclusive, both conditions must be true:
    n >= 10 and n <= 20.
  2. Step 2: Combine conditions with logical AND

    Use && to ensure both conditions hold simultaneously.
    So the correct condition is n >= 10 && n <= 20.
  3. Final Answer:

    n >= 10 && n <= 20 -> Option A
  4. Quick Check:

    Range check uses AND (&&) [OK]
Hint: Use && to combine range checks for inclusive conditions [OK]
Common Mistakes:
  • Using || which allows values outside the range
  • Using strict > and < instead of >= and <=
  • Negating conditions incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard