Rust Booleans
🦀 Rust Booleans
🔹 1. Boolean Data Type (bool)
Rust has only one Boolean type: bool.
✔ Only two values: true and false
🔹 2. Booleans from Comparisons
Most booleans come from comparison operators.
Common comparisons:
-
==equal -
!=not equal -
<,>,<=,>=
🔹 3. Booleans in if Conditions
Rust only allows boolean expressions in conditions.
❌ This is NOT allowed:
🔹 4. Logical Operators with Booleans
| Operator | Meaning |
|---|---|
&& |
AND |
| ` | |
! |
NOT |
🔹 5. Boolean Expressions
Rust treats if as an expression, not just a statement.
Rust way (short and sweet):
Seedhi baat, no bakwaas 😄.
🔹 6. Boolean in Loops
🔹 7. Boolean with match
🔹 8. Boolean vs Integer (Important Difference)
| Language | Allowed |
|---|---|
| C / C++ | if(1) ✔ |
| Rust | if(true) ✔ |
| Rust | if(1) ❌ |
Rust bolta hai: galat hai to galat hai, compromise nahi karega 💪.
🔑 Key Points to Remember
-
boolhas onlytrueorfalse -
No implicit conversion from int to bool
-
Conditions must be boolean
-
Logical operators work only on booleans
