0
0
Rustprogramming~5 mins

Boolean type in Rust - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What values can a Boolean type hold in Rust?
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
How do you declare a Boolean variable named flag set to false?
Alet flag = false;
Bbool flag = false;
Cflag = false;
Dlet flag: bool = false;
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
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"); }
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>
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.