Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Rust Booleans


Booleans

Very often, in programming, you will need a data type that can only have one of two values, like:

  • YES / NO
  • ON / OFF
  • TRUE / FALSE

For this, Rust has a bool data type, which is known as booleans.

Booleans represent values that are either true or false.


Creating Boolean Variables

You can store a boolean value in a variable using the bool type:

Example

let is_programming_fun: bool = true;
let is_fish_tasty: bool = false;

println!("Is Programming Fun? {}", is_programming_fun);
println!("Is Fish Tasty? {}", is_fish_tasty);
Try it Yourself »

Remember that Rust is smart enough to understand that true and false values are boolean values, meaning that you don't have to specify the bool keyword:

Example

let is_programming_fun = true;
let is_fish_tasty = false;

println!("Is Programming Fun? {}", is_programming_fun);
println!("Is Fish Tasty? {}", is_fish_tasty);
Try it Yourself »

Boolean from Comparison

Most of the time, there is no need to type true or false yourself. Instead, boolean values come from comparing values using operators like == or >:

Example

let age = 20;
let can_vote = age >= 18;

println!("Can vote? {}", can_vote);
Try it Yourself »

Here, age >= 18 returns true, as long as age is 18 or older.


Using Booleans in if Statements

Boolean values are often used in if statements to decide what code should run:

Example

let is_logged_in = true;

if is_logged_in {
  println!("Welcome back!");
} else {
  println!("Please log in.");
}
Try it Yourself »

Cool, right? Booleans are the basis for all Rust comparisons and conditions. You will learn more about if and else statements in the next chapter.


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.