A Boolean type in Rust can hold only two values: true or false.
Click to reveal answer
beginner
How do you declare a Boolean variable in Rust?
You declare a Boolean variable using <code>let</code> keyword and specify the type as <code>bool</code>. Example: <code>let is_active: bool = true;</code>
Click to reveal answer
intermediate
What is the default Boolean value if you do not initialize it in Rust?
In Rust, variables must be initialized before use. There is no default value for Boolean variables; you must assign true or false explicitly.
Click to reveal answer
beginner
How can Boolean values be used in control flow in Rust?
Boolean values control the flow of programs using if statements, while loops, and other conditional expressions. For example: if is_active { println!("Active"); }
Click to reveal answer
intermediate
Can you convert other types to Boolean in Rust directly?
Rust does not allow implicit conversion to Boolean. You must explicitly compare or convert values to get a Boolean. For example, <code>let is_zero = x == 0;</code> returns a Boolean.
Click to reveal answer
Which of the following is a valid Boolean value in Rust?
Atrue
B1
C"true"
Dyes
✗ Incorrect
Rust Boolean values are only true or false. Numbers or strings are not valid Boolean values.
How do you declare a Boolean variable named flag set to false?
Alet flag = false;
Bbool flag = false;
Cflag = false;
Dlet flag: bool = false;
✗ Incorrect
Both A (with type inference) and D (with explicit : bool) correctly declare a Boolean variable in Rust.
What happens if you try to use an uninitialized Boolean variable in Rust?
AIt causes a compile-time error
BIt defaults to true
CIt defaults to false
DIt causes a runtime error
✗ Incorrect
Rust requires variables to be initialized before use, so using an uninitialized Boolean causes a compile-time error.
Which statement correctly uses a Boolean in an if condition?
Aif (is_ready) then { println!("Ready"); }
Bif is_ready == true print("Ready");
Cif is_ready { println!("Ready"); }
Dif is_ready = true { println!("Ready"); }
✗ Incorrect
Rust uses if condition { } syntax. Option C is correct and idiomatic.
Can you write let b: bool = 1; in Rust?
AYes, 1 is true
BNo, Rust does not allow implicit conversion from integer to bool
CYes, but only in unsafe code
DYes, if you use <code>as bool</code>
✗ Incorrect
Rust does not allow implicit or explicit conversion from integer to Boolean. You must use comparisons to get a Boolean.
Explain how Boolean types are used in Rust and how you declare them.
Think about how you create a true/false variable and what rules Rust has about initialization.
You got /4 concepts.
Describe how Rust handles conversions to Boolean and why implicit conversion is not allowed.
Consider how Rust keeps your code safe by requiring clear true/false expressions.
You got /4 concepts.
Practice
(1/5)
1. What values can a Rust bool type hold?
easy
A. Only true or false
B. Any number
C. Any string
D. Only 0 or 1
Solution
Step 1: Understand the bool type in Rust
The bool type in Rust is designed to hold only two values: true or false.
Step 2: Compare with other options
Other options like numbers or strings are not valid for bool. Rust does not treat 0 or 1 as boolean values directly.
Final Answer:
Only true or false -> Option A
Quick Check:
Rust bool = true or false [OK]
Hint: Remember: bool means true or false only [OK]
Common Mistakes:
Thinking bool can hold numbers
Confusing bool with integers 0 or 1
Assuming bool can hold strings
2. Which of the following is the correct way to declare a boolean variable in Rust?
easy
A. let is_active: bool = true;
B. bool is_active = true;
C. var is_active = true;
D. let is_active = boolean true;
Solution
Step 1: Recall Rust variable declaration syntax
In Rust, variables are declared with let, and the type follows the variable name with a colon, like let 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 A
Quick Check:
Rust bool variable = let name: bool = value; [OK]
Hint: Use 'let var: bool = true;' to declare booleans [OK]
Common Mistakes:
Using C-style declarations
Using 'var' keyword (not Rust)
Writing 'boolean' instead of 'bool'
3. What will be the output of this Rust code?
fn main() {
let a = true;
let b = false;
println!("{}", a && b);
}
medium
A. true
B. 1
C. false
D. Compilation error
Solution
Step 1: Understand the boolean AND operator
The expression a && b returns true only if both a and b are true. Here, a is true and b is false.
Step 2: Evaluate the expression
Since b is false, a && b evaluates to false. The println! macro prints this value.
In Rust, boolean literals are lowercase: true and false. Using True causes a compilation error.
Step 2: Verify other syntax
The variable name is valid, semicolon is present after println!, and bool can be assigned directly.
Final Answer:
'True' should be lowercase 'true' -> Option D
Quick Check:
Rust bool literals are lowercase true/false [OK]
Hint: Use lowercase true/false in Rust [OK]
Common Mistakes:
Writing True or False with uppercase
Thinking semicolon is missing
Confusing variable name rules
5. Given a variable score of type i32, which Rust expression correctly sets passed as a boolean indicating if score is 50 or more?
hard
A. let passed: bool = score > 50 || false;
B. let passed: bool = score >= 50;
C. let passed: bool = if score >= 50 { true };
D. let passed: bool = score == true;
Solution
Step 1: Understand boolean expressions with comparisons
The expression score >= 50 returns a boolean true if score is 50 or more, otherwise false.
Step 2: Evaluate each option
let passed: bool = score >= 50; directly assigns the comparison result to passed. let passed: bool = score > 50 || false; is incorrect because score > 50 || false excludes 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 B
Quick Check:
Comparison returns bool, assign directly [OK]
Hint: Use comparison directly to get bool result [OK]
Common Mistakes:
Using if without an else branch for assignment
Comparing int to bool directly
Using wrong comparison operators
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.