0
0
Rustprogramming~5 mins

Boolean type in Rust

Choose your learning style9 modes available
Introduction

Boolean type helps us store simple true or false values. It is useful when we want to make decisions in our programs.

Checking if a light is on or off in a smart home system.
Deciding if a user is logged in or not on a website.
Determining if a number is even or odd.
Controlling if a door is locked or unlocked.
Verifying if a form input is valid before submitting.
Syntax
Rust
let variable_name: bool = true; // or false

The Boolean type in Rust is called bool.

It can only hold two values: true or false.

Examples
This creates a Boolean variable named is_raining and sets it to true.
Rust
let is_raining: bool = true;
This creates a Boolean variable named is_open and sets it to false.
Rust
let is_open: bool = false;
Rust can guess the type bool if you assign true or false directly.
Rust
let is_adult = true; // Rust can infer the type
Sample Program

This program uses two Boolean variables to decide what messages to print. It shows how true and false control the flow.

Rust
fn main() {
    let is_sunny: bool = true;
    let is_weekend = false;

    if is_sunny {
        println!("It is sunny outside.");
    } else {
        println!("It is not sunny outside.");
    }

    if !is_weekend {
        println!("Time to work!");
    } else {
        println!("Enjoy your weekend!");
    }
}
OutputSuccess
Important Notes

Boolean values are often used in if statements to control what the program does.

The ! symbol means "not" and flips the Boolean value.

Summary

Boolean type stores true or false values.

Use Booleans to make decisions in your code.

Rust calls this type bool and it is simple but very useful.