Concept Flow - Boolean type
Start
Declare bool variable
Assign true or false
Use in condition or print
Program ends
This flow shows how a boolean variable is declared, assigned true or false, then used in conditions or output.
fn main() {
let is_rust_fun: bool = true;
println!("Is Rust fun? {}", is_rust_fun);
}| Step | Action | Variable | Value | Output |
|---|---|---|---|---|
| 1 | Declare variable is_rust_fun | is_rust_fun | uninitialized | |
| 2 | Assign true to is_rust_fun | is_rust_fun | true | |
| 3 | Print is_rust_fun value | is_rust_fun | true | Is Rust fun? true |
| 4 | Program ends |
| Variable | Start | After Step 1 | After Step 2 | Final |
|---|---|---|---|---|
| is_rust_fun | none | uninitialized | true | true |
Boolean type in Rust:
- Declared with 'bool' keyword
- Values: true or false only
- Used in conditions and printing
- Cannot assign numbers directly
- Print with println!("{}", var)bool type hold?bool type in Rustbool type in Rust is designed to hold only two values: true or false.bool. Rust does not treat 0 or 1 as boolean values directly.true or false -> Option Abool = true or false [OK]let, and the type follows the variable name with a colon, like let var_name: type = value;.fn main() {
let a = true;
let b = false;
println!("{}", a && b);
}a && b returns true only if both a and b are true. Here, a is true and b is false.b is false, a && b evaluates to false. The println! macro prints this value.fn main() {
let flag: bool = True;
println!("{}", flag);
}true and false. Using True causes a compilation error.println!, and bool can be assigned directly.score of type i32, which Rust expression correctly sets passed as a boolean indicating if score is 50 or more?score >= 50 returns a boolean true if score is 50 or more, otherwise false.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.Pass the test and this pattern moves unknown