Rust Booleans
Programs constantly need to answer yes/no questions: Is the user logged in? Is the list empty? Has the player won? Rust represents these answers with the bool type, which has exactly two possible values: true and false.
Boolean values
fn main() {
let door_open = true;
let alarm_on: bool = false;
println!("Door open: {}", door_open);
println!("Alarm on: {}", alarm_on);
}Door open: true
Alarm on: false
A bool takes one byte of memory. Unlike some languages, Rust does not treat numbers as booleans: if 1 { ... } is a compile error. A condition must be a real bool.
Booleans from comparisons
Most booleans are not typed in by hand — they come from comparing values. Any comparison operator (==, !=, <, >, <=, >=) produces a bool that you can print, store or use in a condition.
fn main() {
let age = 17;
let voting_age = 18;
let can_vote = age >= voting_age;
println!("Can vote: {}", can_vote);
let name = "Rust";
println!("Name is Rust: {}", name == "Rust");
println!("Name has 4 letters: {}", name.len() == 4);
}Can vote: false
Name is Rust: true
Name has 4 letters: true
Combining booleans
Use the logical operators to build more complex conditions:
| Expression | Result |
|---|---|
true && true |
true |
true && false |
false |
true || false |
true |
false || false |
false |
!true |
false |
!false |
true |
fn main() {
let has_ticket = true;
let is_vip = false;
let banned = false;
let may_enter = (has_ticket || is_vip) && !banned;
let front_row = has_ticket && is_vip;
println!("May enter: {}", may_enter);
println!("Front row seat: {}", front_row);
}May enter: true
Front row seat: false
Methods that return booleans
Many methods in the standard library answer a question with a bool. Their names usually start with is_, has_, contains or starts_with, so the code reads almost like English:
| Method | Asks |
|---|---|
n.is_positive() |
Is the number greater than zero? |
c.is_uppercase() |
Is the character an uppercase letter? |
s.is_empty() |
Is the string empty? |
s.contains("x") |
Does the string contain "x"? |
s.starts_with("ab") |
Does the string begin with "ab"? |
fn main() {
let password = "Secret123";
let long_enough = password.len() >= 8;
let has_digit = password.contains('1') || password.contains('2') || password.contains('3');
let starts_upper = password.starts_with('S');
println!("Long enough: {}", long_enough);
println!("Has a digit: {}", has_digit);
println!("Starts with S: {}", starts_upper);
println!("Accepted: {}", long_enough && has_digit);
}Long enough: true
Has a digit: true
Starts with S: true
Accepted: true
Tip: Give boolean variables names that read as a question or a statement, like
is_ready,has_errorsorcan_vote. Thenif is_ready { ... }reads naturally.
Booleans and numbers
You can convert a bool into an integer with as: true as i32 is 1 and false as i32 is 0. This is occasionally handy for counting how many conditions are true. The opposite direction does not exist — to turn a number into a bool, compare it, for example count != 0.
fn main() {
let checks = [true, false, true, true];
let passed = checks[0] as i32 + checks[1] as i32 + checks[2] as i32 + checks[3] as i32;
println!("{} of 4 checks passed", passed);
}3 of 4 checks passed
Booleans become really powerful when they drive decisions in your program, which is exactly what the next chapter, If..Else, is about.
Test yourself
Which values can a bool hold?
- 0 and 1
- true and false
- yes and no
- Any integer
What does 10 > 3 && 2 > 5 evaluate to?
- true
- false
- 10
- Compile error
Is if 1 { } valid Rust?
- Yes, 1 means true
- No, the condition must be a
bool - Only in release builds
- Yes, any non-zero number works