structure Boolean in Rust 1.55

Find this useful? Support us: Star on GitHub 6
Category: Other structure | Language: Rust 1.55

Boolean in Rust represents truth values, which can either be true or false. Boolean values are represented using the bool type in Rust, and they play a very important role in programming as they help in the evaluation of logical expressions.

Here are some examples of how to use boolean in Rust:

1. To declare a variable of boolean type:

let x: bool = true;
let y: bool = false;

2. To declare a function that returns a boolean value:

fn is_even(num: i32) -> bool {
    if num % 2 == 0 {
        return true;
    } else {
        return false;
    }
}

3. To use boolean values in control flow statements:

let num = 5;
if num < 10 {
    println!("{} is less than 10", num);
} else {
    println!("{} is greater than or equal to 10", num);
}

4. To use boolean values in logical expressions:

let a = true;
let b = false;
println!("a AND b is {}", a && b);
println!("a OR b is {}", a || b);
println!("NOT a is {}", !a);

In these examples, the && operator represents logical and, || represents logical or, and ! represents logical not.

Using boolean values in Rust is simple and intuitive, and they can help make your programs more efficient and effective.