Rust Booleans
A boolean is the simplest type there is: it can only be true or false. In Rust it’s called bool, and you’ll use it everywhere you need to make a decision in your code.
Declaring a boolean
Section titled “Declaring a boolean”fn main() { let is_active = true; let has_error: bool = false; println!("active: {is_active}, error: {has_error}");}Rust almost always infers bool on its own from true/false, so you rarely need to write the type explicitly.
Booleans usually come from comparisons
Section titled “Booleans usually come from comparisons”You don’t often write true or false directly in real code — they usually come from comparing something:
fn main() { let age = 20; let can_vote = age >= 18; println!("Can vote: {can_vote}");}Output:
Can vote: trueUsing booleans in conditions
Section titled “Using booleans in conditions”The main job of a bool is deciding which branch of an if to run:
fn main() { let is_raining = true;
if is_raining { println!("Take an umbrella"); } else { println!("Leave it at home"); }}We cover if properly on the If…Else page — this is just a taste of why booleans matter.
Combining booleans and short-circuiting
Section titled “Combining booleans and short-circuiting”You met && (and), || (or) and ! (not) on the Operators page. There’s one more thing to know about them: they stop early when the answer is already decided. This is called short-circuit evaluation.
- With
a && b, ifaisfalse, the whole thing must befalse, so Rust never looks atb. - With
a || b, ifaistrue, the whole thing must betrue, so Rust never looks atb.
That’s more than a speed trick. It lets the left side protect the right side:
fn main() { let count = 0; let total = 50;
// count is 0, so Rust stops after `count != 0` and never divides. let big_average = count != 0 && total / count > 10; println!("big average: {big_average}");
let is_weekend = true; let has_homework = true; // is_weekend is true, so `!has_homework` is never checked. let can_relax = is_weekend || !has_homework; println!("can relax: {can_relax}");}Output:
big average: falsecan relax: trueDividing by zero would crash the program. Because count != 0 comes first and is false, the division simply never runs. Swap the two sides (total / count > 10 && count != 0) and the division would run first. Here the compiler even spots it and refuses to build (“this operation will panic at runtime”); with a value that only arrives while the program runs, it would crash instead. So put the “safety check” on the left.
Practice
Section titled “Practice”1. Can they ride?
Section titled “1. Can they ride?”A theme park ride lets you on if you are at least 120 cm tall, or if you are at least 8 years old and riding with an adult. Store a height of 115, an age of 9 and with_adult = true, then print whether this child can ride. Hint: use parentheses to group the “age and adult” part.
Show solution
fn main() { let height_cm = 115; let age = 9; let with_adult = true;
let tall_enough = height_cm >= 120; let can_ride = tall_enough || (age >= 8 && with_adult);
println!("tall enough: {tall_enough}"); println!("can ride: {can_ride}");}Output:
tall enough: falsecan ride: true2. Leap years
Section titled “2. Leap years”A year is a leap year if it divides by 4 but not by 100, or if it divides by 400. Work out a boolean is_leap for 2024, print it, then do the same for 1900. Hint: “divides by 4” is year % 4 == 0, and you can shadow year and is_leap with a second let.
Show solution
fn main() { let year = 2024; let is_leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; println!("{year} is a leap year: {is_leap}");
let year = 1900; let is_leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; println!("{year} is a leap year: {is_leap}");}Output:
2024 is a leap year: true1900 is a leap year: falseboolholds exactly two values:trueorfalse.- Comparisons (
==,<,>=, …) produce booleans. - Conditions in
ifmust be a realbool— no implicit conversions from numbers or strings. &&and||short-circuit: they skip the right side once the answer is known, so put safety checks on the left.
Quick check
Section titled “Quick check”Quick check
1. What type does a comparison like age >= 18 produce?
2. In Rust, can you use an integer like 0 directly as a condition in an if, the way you might in some other languages?
3. In count != 0 && total / count > 10, what happens when count is 0?
Score: 0 / 3
Read more
Section titled “Read more”- The Boolean Type — The Rust Book: the official short introduction to
bool, alongside the other basic types. bool— standard library docs: every method onbool, such asthenandthen_some, for when you’re ready for them.- Lazy boolean operators — The Rust Reference: the exact rules for how
&&and||skip their right side. - Primitives — Rust by Example:
boolnext to Rust’s other built-in types, with a runnable example.