Rust Booleans

In the Rust programming language, a Boolean is a data type that can have one of two values: true or false.

Booleans are typically used to represent the truthiness of a statement or condition, i.e. whether or not a statement is true.

The size of a Boolean in Rust is one byte, regardless of value (true or false).

Here’s an example of Boolean variables in Rust:

let a = false;
let b = true;

In this example, a and b are both variables with the type of bool. The variable a is a bool with a value of ‘false’, and b is a bool with the value true.

Booleans are commonly used in control flow statements, such as if expressions and while loops. We can also use logical operators, such as && (and), || (or), and ! (not), to combine and manipulate Boolean values.

In this article, we will cover Boolean variables, including: using Boolean in if…else and while control flow statements, using Boolean operators, combining Boolean operators, and converting a Boolean value to an integer.

Booleans in Control Flow Statements

Booleans are commonly used in control flow statements such as if…else conditional expressions and while loops.

In both cases, the evaluation of a Boolean value determined whether a code block will run or loop. In other words, if a given statement is true, then a certain block of code will run, sometimes multiple times.

This allows us to see the power of Boolean variables and their usefulness beyond basic logical functions.

If…Else

An if…else expression is a conditional statement. Essentially, it says “if something is true, then run this block of code. If it is false, run a different block of code”.

If expressions have the following syntax in Rust:

let b = true;

if b {
    // do something
} else {
    // do something else
}

In this code, the if expression checks if b is true or false.

If it is true, the code inside the first block (// do something) will be executed. If it is false, the code inside the else block (// do something else) will be executed instead.

Main article: If expressions

While

A while loop is used to perform a finite number of iterations based on the evaluation of a Boolean statement.

Here’s an example of a while loop that uses a Boolean to control the loop:

let mut b = true;

while b {
    // do something
    b = false;
}

In this code, the while loop will continue to run as long as the variable b is true. Inside the loop, the code does something and then sets b to false, so the loop will only run once.

Main article: While loops

Using Boolean Operators in Rust

We can use Boolean logical operators, such as && (and), || (or), and ! (not), to combine and manipulate Boolean values.

&& AND Operator

We can use the && operator to perform a logical “AND” operation on two Boolean values.

The && operator returns true if both operands are true, and false if either or both are false.

Here’s an example showing the && operator in an if expression:

let b1 = true;
let b2 = false;

if b1 && b2 {
    // do something
}

In this example, the if expression checks the result of the && operator. Since b1 is true and b2 is false, the condition evaluates to false and the code inside the if block will not be executed.

We can also use the && operator in a while loop to control the loop:

let mut b1 = true;
let mut b2 = false;

while b1 && b2 {
    // do something
    b1 = false;
}

In this example, the while loop will continue to run as long as the values of b1 and b2 are both true. Inside the loop, the code does something and then sets b1 to false, so the loop will only run once.

|| OR Operator

Similar to the && (AND) operator, we can use the || operator to perform a logical “or” operation on two Boolean values.

The || operator returns true if either operand is true. It returns false only if both operands are false.

For example:

let b1 = true;
let b2 = false;

if b1 || b2 {
    // do something
}

In this example, the if expression checks the result of the || operator. Since b1 is true, the condition evaluates to true. The code inside the if block will be executed.

We can also use the || operator in a while loop to control the loop:

let mut b1 = true;
let mut b2 = false;

while b1 || b2 {
    // do something
    b1 = false;
    b2 = true;
}

In this code, the while loop will continue to run as long as the value of b1 is true or the value of b2 is true. Inside the loop, the code does something and then sets b1 to false and b2 to true, so the loop will run twice.

! NOT Operator

The ! (NOT) operator reverses the value of the operand:

  • !true evaluates to false
  • !false evaluates to true.

Here’s an example of using the ! operator in an if expression:

let b = true;

if !b {
    // do something
}

The if expression checks the result of the ! operator, which reverses the value of b.

Since b is true, the condition evaluates to false and the code inside the if block is not executed.

You can also use the ! operator in a while loop to control the loop:

let mut b = true;

while !b {
    // do something
    b = true;
}

In this code, the while loop will continue to run as long as the value of b is false. Inside the loop, the code does something and then sets b to true, so the loop will only run once.

Combining Boolean Operators in Rust

In Rust, we can combine Boolean values to create more complex expressions.

For example:

let b1 = true;
let b2 = false;
let b3 = true;

if (b1 && b2) || b3 {
    // do something
}

The if expression here combines the values of b1, b2, and b3 using the && (AND) and || (OR) operators.

The logics reads ‘IF (b1 AND b2) OR b3’. The expression (b1 AND b2) is evaluated independently, and is false because b2 is false.

The logic can be simplified to ‘IF (false) OR b3’. This evaluates to true because b3 is true.

Boolean Default in Rust

In Rust, the default value for a Boolean variable is false.

This means that if you declare a bool variable without initializing it, it will by default have the value false.

For example:

let b: bool; // b is declared but not initialized

println!("{}", b); // this will print "false" to the console

In this code, we declared a bool variable ‘b‘ without initializing it.

Because b is not initialized, it will have the default value of false. When we print the value of b using println!, it will print “false” to the console.

You can also initialize a bool variable with a specific value, such as true or false. In this case, the default value of the variable will be overwritten with the specified value.

Boolean to Integer

We can convert a Boolean value to an integer using the as operator. The as operator is used to perform type conversions in Rust.

A Boolean value of ‘true’ will be converted into an integer value of ‘1’, while a boolean value of ‘false’ will be converted into an integer value of ‘0’.

let b = true; 
let i: i32 = b as i32;

println!("{}", i); // prints "1"

In this code, we declared a bool variable ‘b‘ and initialized it with the value true.

We then declared an i32 (32-bit signed integer) variable called i to hold the integer converted value of b.

To convert b to an i32, we use the as operator and specify the type we want to convert into. This converts the value of b from true to 1, which is the corresponding integer value of true.

We can also convert a Boolean to an integer using the to_string() method.

This method first converts a Boolean to a string. Once we have a string, we can convert it to an integer using the parse() method.

let b = true;
let i: i32 = b.to_string().parse().unwrap();

println!("{}", i); // prints "1"

In this example, we used the to_string() method to convert b to a string.

Then we used parse() to convert the string to an integer. The parse() method returns a Result object, which we need to handle in order to get the value of the integer.

In this case, we used the unwrap() method to unwrap the value from the Result object and assign it to i.