Chapter 11: Rust Booleans
Rust Booleans
Booleans are one of the simplest yet most powerful parts of any programming language — they answer yes/no, on/off, true/false questions that control almost everything in your code (if statements, loops, flags, etc.). Rust keeps bool clean, safe, and predictable — no weird tricks like in some languages.
Let me explain it like your friendly teacher sitting next to you: slowly, with lots of examples you can copy-paste and cargo run, everyday analogies (Hyderabad traffic lights, biryani spice level 😄), and comparisons to other languages you might know.
1. What is the Boolean Type in Rust?
- The type is called bool (lowercase — it’s a keyword in the prelude)
- It can have exactly two values:
- true
- false
- Size: 1 byte (8 bits) in memory — even though logically it’s just 1 bit, Rust uses a full byte for speed and alignment reasons (very common in modern languages)
- Representation:
- false → bit pattern 0x00 (all bits zero)
- true → bit pattern 0x01 (only the least significant bit set)
- Anything else is undefined behavior — Rust strictly enforces only these two values
Official docs (from Rust Reference & std::primitive.bool) say: “The bool represents a value, which could only be either true or false.”
No null, no 0/1 confusion by default — very strict and safe!
2. Creating Boolean Variables
Rust infers the type automatically from true/false literals — no need to write : bool most times.
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
fn main() { let is_hot_today = true; // inferred as bool let is_raining = false; // Explicit type (good when clarity needed or in complex expressions) let learning_rust: bool = true; println!("Is it hot in Hyderabad? {}", is_hot_today); // true println!("Is it raining? {}", is_raining); // false } |
- Literals: true and false (keywords, lowercase only)
- No True/False like Python — Rust is case-sensitive here
3. Main Uses of Booleans — Controlling Flow
Booleans shine in conditions:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
fn main() { let temperature = 35; let is_hot = temperature > 32; // comparison → bool if is_hot { println!("Summer mode ON in Hyderabad! 🔥☀️"); } else { println!("Nice weather for coding 🦀"); } // Direct use without variable if temperature >= 40 { println!("Extreme heat warning!"); } } |
- if, while, match guards, etc. require a bool — no automatic conversion from int (unlike C/JavaScript/Python)
- Wrong: if 1 { … } → compile error!
- Good: if temperature != 0 { … }
4. Logical Operators on Booleans
Rust gives you the classic three (short-circuiting):
| Operator | Meaning | Example | Result |
|---|---|---|---|
| && | AND (both true) | true && false | false |
|
OR (at least one) | ||
| ! | NOT (invert) | !true | false |
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
fn main() { let has_biryani = true; let has_mirchi = true; let is_spicy_enough = has_biryani && has_mirchi; let has_water = false; let can_eat = is_spicy_enough || has_water; println!("Can eat comfortably? {}", can_eat); // true let is_mild = !is_spicy_enough; println!("Mild version? {}", is_mild); // false // Short-circuit example let result = true || (println!("This won't print!"), false); // Right side skipped because left is true } |
5. Bitwise Boolean Operations (Advanced but Useful)
bool also supports bitwise operators (& | ^) — treats true as 1, false as 0.
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
fn main() { let a = true; let b = false; println!("Bitwise AND: {}", a & b); // false println!("Bitwise OR: {}", a | b); // true println!("Bitwise XOR: {}", a ^ b); // true (different) } |
Rare for plain bool, but useful when packing flags into integers or working with bitfields.
6. Converting bool to Integer (Rare, but Defined)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
fn main() { let t = true; let f = false; let t_as_int: i32 = t as i32; // 1 let f_as_int: i32 = f as i32; // 0 println!("true as int: {}, false as int: {}", t_as_int, f_as_int); } |
- Official: true → 1, false → 0
- No automatic bool ←→ int conversion in conditions (safety!)
7. Common Patterns & Tips
- Flag variables: let is_active: bool = true;
- Early returns: if !condition { return; }
- Negation: Prefer !is_valid over is_invalid = !is_valid when possible
- No truthy/falsy coercion: if some_string.len() > 0 { … } (explicit & safe)
Full Small Example to Try
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
fn main() { let current_temp = 36; let is_summer = current_temp > 30; let has_fan = true; let feels_comfortable = !is_summer || has_fan; println!("Current temp: {}°C", current_temp); println!("Is summer? {}", is_summer); println!("Has fan? {}", has_fan); println!("Feels comfortable? {}", feels_comfortable); // true if feels_comfortable { println!("Good day to learn Rust in Hyderabad! 🦀🚀"); } else { println!("Too hot — need AC first! 😅"); } } |
Teacher Summary
Rust Booleans (bool) =
- Exactly two values: true or false
- 1 byte in size
- No automatic conversions — forces explicit conditions
- Used everywhere for decisions (if, while, flags)
- Logical operators && || ! short-circuit
- Bitwise & | ^ also available
- Super safe: no “0 is false” tricks, no undefined values
This simplicity + strictness is one reason Rust code feels reliable — the compiler catches sloppy boolean logic early.
Next topic?
- Control flow (if/else, match — booleans everywhere)?
- Option & Result (Rust’s safe alternatives to null/true-error flags)?
- Or loops (where booleans control while/for)?
Just tell me — your Rust class is rocking! 🦀🚀
