Rust Syntax & Basic Constructs
Estimated reading: 3 minutes 31 views

🦀 Rust – Booleans: Logic Control with true and false

🧲 Introduction – Why Learn Booleans in Rust?

Booleans are a fundamental part of logic control in Rust. Whether you’re making decisions with if statements, handling pattern matching, or looping, you’ll need to work with bool values effectively.

🎯 In this guide, you’ll learn:

  • How to declare and use boolean variables
  • Boolean expressions and comparison operators
  • How booleans interact with if, match, and control flow
  • Common use cases and logic-based patterns in Rust

🔘 Boolean Type in Rust

Rust uses the bool type with two values:

let is_rust_fun: bool = true;
let is_boring = false;

📌 By default, bool values are immutable unless declared with mut.


🧪 Boolean Expressions with Comparisons

fn main() {
    let age = 18;
    let is_adult = age >= 18;

    println!("Is adult? {}", is_adult);
}
OperatorDescriptionExample
==Equalsa == b
!=Not equalsa != b
>Greater thana > b
<Less thana < b
>=Greater or equala >= b
<=Less or equala <= b

🔗 Logical Operators

fn main() {
    let x = true;
    let y = false;

    println!("AND: {}", x && y); // false
    println!("OR: {}", x || y);  // true
    println!("NOT x: {}", !x);   // false
}
OperatorDescriptionExample
&&Logical ANDx && y
``
!Logical NOT!x

🔁 Use Booleans in Control Flow

✅ If-Else Example

fn main() {
    let has_access = true;

    if has_access {
        println!("Access granted.");
    } else {
        println!("Access denied.");
    }
}

✅ Match with Booleans

fn main() {
    let is_active = false;

    match is_active {
        true => println!("Active!"),
        false => println!("Inactive!"),
    }
}

🧠 Boolean Return from Functions

fn is_even(num: i32) -> bool {
    num % 2 == 0
}

fn main() {
    println!("Is 6 even? {}", is_even(6));
}

⚠️ Common Boolean Mistakes

MistakeProblemFix
Using = instead of ==Assignment instead of comparisonUse == for logical comparisons
Using non-bool in if conditionRust only accepts bool (not integers)Must be true or false, not 1/0
Forgetting parentheses in complex expressionsCompile error or logic bugUse () to group logical operations

❌ Unlike C or Python, Rust does not auto-convert 0 to false or 1 to true.


📌 Summary – Recap & Next Steps

Booleans in Rust power your decision-making logic. They are type-safe, explicit, and enforced by the compiler to prevent common logic errors seen in other languages.

🔍 Key Takeaways:

  • Rust’s bool type only has true or false
  • Use comparison (==, !=) and logical (&&, ||, !) operators
  • Booleans work cleanly with if, match, and function returns
  • Rust does not allow implicit boolean coercion from integers

⚙️ Next: Explore Rust – Operators to understand arithmetic, logical, and bitwise operations in Rust.


❓FAQs


Does Rust allow using numbers in if conditions like C or Python?
❌ No. Rust strictly requires a bool value in if conditions. You cannot use integers directly.


Can I return a boolean from a function?
✅ Yes. Rust functions can return bool types just like any other type.


What is the type of true in Rust?
✅ The type is bool. It’s a built-in primitive type in Rust.


Is bool mutable in Rust?
✅ Like other variables, bool is immutable by default. Use mut if you want to change its value.

let mut flag = true;
flag = false;

Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Rust – Booleans

Or Copy Link

CONTENTS
Scroll to Top