Boolean Type

A Boolean is a type that is represented by either a value of one or a value of zero. To make it easier to use the values have been given names: true & false.

Boolean values are typically used for conditional logic or validation, for example in if expressions, and thus expressions are said to be evaluated to true or false.

Using the unary operator ! the Boolean value can be changed:

  • From true to false
  • From false to true

Example

The following example creates two Boolean variables, performs a comparison using the unary operator ! and implicitly returns the result.

fn returns_true() -> bool {
    let is_true = true;
    let is_false = false;

    // implicitly returns the Boolean value of `true`
    is_true == !is_false
}