Rust Booleans
Working with true and false values in Rust
✅ What are Rust Booleans?
Boolean values in Rust represent true or false states. They're essential for decision-making, conditions, and logical operations. Rust's bool type ensures type safety and clear program logic.
// Boolean basics
let is_active: bool = true;
let is_complete = false;
let result = 5 > 3; // true
Boolean Characteristics
Two Values Only
Can only be true or false
let yes: bool = true;
let no: bool = false;
From Comparisons
Result of comparison operations
let is_equal = 5 == 5; // true
let is_greater = 10 > 5; // true
Logical Operations
Combine with logical operators
let result = true && false; // false
let other = true || false; // true
Control Flow
Used in if statements and loops
if is_active {
println!("System is running");
}
🔹 Creating Boolean Values
Different ways to create and work with boolean values:
fn main() {
// Direct assignment
let is_sunny: bool = true;
let is_raining = false;
// From comparisons
let is_hot = 30 > 25; // true
let is_cold = 10 > 20; // false
let is_equal = "hello" == "hello"; // true
// From function calls
let text = "Rust";
let is_empty = text.is_empty(); // false
let starts_with_r = text.starts_with("R"); // true
println!("Sunny: {}", is_sunny);
println!("Raining: {}", is_raining);
println!("Hot: {}", is_hot);
println!("Cold: {}", is_cold);
println!("Equal: {}", is_equal);
println!("Empty: {}", is_empty);
println!("Starts with R: {}", starts_with_r);
}
Output:
Sunny: true
Raining: false
Hot: true
Cold: false
Equal: true
Empty: false
Starts with R: true
🔹 Boolean Operations
Combining boolean values with logical operators:
fn main() {
let has_license = true;
let has_car = false;
let has_gas = true;
let is_weekend = true;
// AND operator (&&) - all must be true
let can_drive = has_license && has_car && has_gas;
println!("Can drive: {}", can_drive);
// OR operator (||) - at least one must be true
let can_relax = is_weekend || !has_car;
println!("Can relax: {}", can_relax);
// NOT operator (!) - flips the value
let is_weekday = !is_weekend;
println!("Is weekday: {}", is_weekday);
// Complex expressions
let good_day = (is_weekend && has_gas) || (has_license && !has_car);
println!("Good day: {}", good_day);
}
Output:
Can drive: false
Can relax: true
Is weekday: false
Good day: true
🔹 Boolean in Conditions
Using booleans for decision making and control flow:
fn main() {
let age = 18;
let has_id = true;
let is_student = false;
// Simple boolean conditions
let is_adult = age >= 18;
let can_enter = is_adult && has_id;
if can_enter {
println!("Welcome! You can enter.");
} else {
println!("Sorry, you cannot enter.");
}
// Multiple conditions
let discount = if is_student {
0.20 // 20% student discount
} else if age >= 65 {
0.15 // 15% senior discount
} else {
0.0 // No discount
};
println!("Your discount: {}%", discount * 100.0);
// Boolean variables in conditions
if is_student {
println!("Student benefits available!");
}
if !is_student {
println!("Consider our student program!");
}
}
Output:
Welcome! You can enter.
Your discount: 0%
Consider our student program!
🔹 Boolean Functions
Creating functions that return boolean values:
// Functions returning booleans
fn is_even(number: i32) -> bool {
number % 2 == 0
}
fn is_positive(number: i32) -> bool {
number > 0
}
fn is_valid_age(age: i32) -> bool {
age >= 0 && age <= 150
}
fn can_vote(age: i32, is_citizen: bool) -> bool {
age >= 18 && is_citizen
}
fn main() {
let num = 42;
let person_age = 25;
let citizenship = true;
println!("{} is even: {}", num, is_even(num));
println!("{} is positive: {}", num, is_positive(num));
println!("Age {} is valid: {}", person_age, is_valid_age(person_age));
println!("Can vote: {}", can_vote(person_age, citizenship));
// Using boolean functions in conditions
if is_even(num) && is_positive(num) {
println!("{} is a positive even number!", num);
}
}
Output:
42 is even: true
42 is positive: true
Age 25 is valid: true
Can vote: true
42 is a positive even number!