Boolean type in Rust - Time & Space Complexity
When working with Boolean values in Rust, it's helpful to understand how fast operations on them run.
We want to know how the time to process Boolean values changes as we use more of them.
Analyze the time complexity of the following code snippet.
fn count_true(values: &[bool]) -> usize {
let mut count = 0;
for &value in values {
if value {
count += 1;
}
}
count
}
This code counts how many true values are in a list of Booleans.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each Boolean in the list.
- How many times: Once for every item in the input list.
As the list gets longer, the code checks each Boolean one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The number of operations grows directly with the number of Booleans.
Time Complexity: O(n)
This means the time to count true values grows in a straight line as the list gets longer.
[X] Wrong: "Checking Booleans is instant no matter how many there are."
[OK] Correct: Even though each check is fast, doing many checks adds up as the list grows.
Understanding how simple operations like checking Booleans scale helps you explain your code's speed clearly and confidently.
"What if we changed the input to a nested list of Booleans? How would the time complexity change?"
Practice
bool type hold?Solution
Step 1: Understand the
Thebooltype in Rustbooltype in Rust is designed to hold only two values:trueorfalse.Step 2: Compare with other options
Other options like numbers or strings are not valid forbool. Rust does not treat0or1as boolean values directly.Final Answer:
Onlytrueorfalse-> Option AQuick Check:
Rustbool= true or false [OK]
- Thinking bool can hold numbers
- Confusing bool with integers 0 or 1
- Assuming bool can hold strings
Solution
Step 1: Recall Rust variable declaration syntax
In Rust, variables are declared withlet, and the type follows the variable name with a colon, likelet var_name: type = value;.Step 2: Check each option
let is_active: bool = true; uses correct Rust syntax for a boolean variable. Options B and C use syntax from other languages. let is_active = boolean true; is invalid syntax.Final Answer:
let is_active: bool = true; -> Option AQuick Check:
Rust bool variable = let name: bool = value; [OK]
- Using C-style declarations
- Using 'var' keyword (not Rust)
- Writing 'boolean' instead of 'bool'
fn main() {
let a = true;
let b = false;
println!("{}", a && b);
}Solution
Step 1: Understand the boolean AND operator
The expressiona && breturnstrueonly if bothaandbaretrue. Here,aistrueandbisfalse.Step 2: Evaluate the expression
Sincebisfalse,a && bevaluates tofalse. Theprintln!macro prints this value.Final Answer:
false -> Option CQuick Check:
true AND false = false [OK]
- Assuming true && false is true
- Confusing boolean with integer 1 or 0
- Expecting compilation error
fn main() {
let flag: bool = True;
println!("{}", flag);
}Solution
Step 1: Check boolean literal case sensitivity
In Rust, boolean literals are lowercase:trueandfalse. UsingTruecauses a compilation error.Step 2: Verify other syntax
The variable name is valid, semicolon is present afterprintln!, andboolcan be assigned directly.Final Answer:
'True' should be lowercase 'true' -> Option DQuick Check:
Rust bool literals are lowercase true/false [OK]
- Writing True or False with uppercase
- Thinking semicolon is missing
- Confusing variable name rules
score of type i32, which Rust expression correctly sets passed as a boolean indicating if score is 50 or more?Solution
Step 1: Understand boolean expressions with comparisons
The expressionscore >= 50returns a booleantrueifscoreis 50 or more, otherwisefalse.Step 2: Evaluate each option
let passed: bool = score >= 50; directly assigns the comparison result topassed. let passed: bool = score > 50 || false; is incorrect becausescore > 50 || falseexcludes 50 itself. let passed: bool = if score >= 50 { true }; is invalid because the if lacks an else branch and thus doesn't return a value when the condition is false (compilation error). let passed: bool = score == true; compares an integer to a boolean, which is invalid.Final Answer:
let passed: bool = score >= 50; -> Option BQuick Check:
Comparison returns bool, assign directly [OK]
- Using if without an else branch for assignment
- Comparing int to bool directly
- Using wrong comparison operators
Ready to prove it?
Pass the test and this pattern moves unknown
