Logical operators in Rust - Time & Space Complexity
Logical operators combine true/false values to make decisions in code.
We want to see how using these operators affects how long the program takes to run.
Analyze the time complexity of the following code snippet.
fn check_values(a: bool, b: bool, c: bool) -> bool {
a && b || c
}
fn main() {
let result = check_values(true, false, true);
println!("{}", result);
}
This code uses logical AND and OR to combine three true/false values and returns the result.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Evaluating a few logical operators on fixed inputs.
- How many times: Each operator runs once per function call; no loops or repeated steps.
Since the number of logical checks is fixed, the time does not grow with input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 1 | 3 logical checks |
| 10 | 3 logical checks |
| 100 | 3 logical checks |
Pattern observation: The number of operations stays the same no matter how big the input is.
Time Complexity: O(1)
This means the time to run the code stays constant, no matter the input size.
[X] Wrong: "Logical operators take longer as inputs grow because they combine values."
[OK] Correct: Logical operators here only check a fixed number of values, so time does not increase with input size.
Understanding that simple logical checks run in constant time helps you explain code efficiency clearly and confidently.
"What if we changed the function to check logical operators on an array of booleans instead of just three values? How would the time complexity change?"
Practice
Solution
Step 1: Understand the meaning of logical AND
The AND operator returns true only when both conditions it connects are true.Step 2: Identify Rust's AND operator
In Rust, the AND operator is written as&&.Final Answer:
&& -> Option DQuick Check:
AND = && [OK]
- Confusing || (OR) with && (AND)
- Using single & which is bitwise AND
- Mistaking ! for AND
flag in Rust?Solution
Step 1: Recall Rust's NOT operator syntax
Rust uses the exclamation mark!before a boolean to negate it.Step 2: Apply negation to variable
Writingflag!flagreturns the opposite boolean value offlag.Final Answer:
!flag -> Option BQuick Check:
NOT = ! [OK]
- Writing negation after the variable (flag!)
- Using 'not' keyword which Rust does not support
- Using ~ which is bitwise NOT, not boolean NOT
let a = true;
let b = false;
println!("{}", a || b && !a);
Solution
Step 1: Evaluate the expression with operator precedence
AND (&&) has higher precedence than OR (||), so evaluateb && !afirst.
Here,b = false,a = true, so!a = false.
Thus,b && !a = false && false = false.Step 2: Evaluate the OR operation
Now evaluatea || (result)which istrue || false.
This results intrue.Final Answer:
true -> Option CQuick Check:
Operator precedence: && before || = true [OK]
- Evaluating || before &&
- Ignoring negation operator precedence
- Assuming false because b is false
let x = true;
let y = false;
if x & y {
println!("Both true");
}
Solution
Step 1: Check the operator used in the if condition
The code uses&which is a bitwise AND operator, not logical AND.Step 2: Identify correct logical AND operator in Rust
Logical AND requires&&to combine boolean values properly.Final Answer:
Using & instead of && for logical AND -> Option AQuick Check:
Logical AND needs &&, not & [OK]
- Confusing bitwise & with logical &&
- Assuming & works for booleans
- Ignoring compiler warnings about operator types
n is between 10 and 20 inclusive in Rust. Which condition correctly uses logical operators?Solution
Step 1: Understand the range condition
To check ifnis between 10 and 20 inclusive, both conditions must be true:n >= 10andn <= 20.Step 2: Combine conditions with logical AND
Use&&to ensure both conditions hold simultaneously.
So the correct condition isn >= 10 && n <= 20.Final Answer:
n >= 10 && n <= 20 -> Option AQuick Check:
Range check uses AND (&&) [OK]
- Using || which allows values outside the range
- Using strict > and < instead of >= and <=
- Negating conditions incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
