> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Rust bool

The `bool` type in Rust is a primitive data type representing a boolean logic value that can exist in exactly one of two states: `true` or `false`.

## Control Flow and Strict Evaluation

In Rust, control flow constructs such as `if` and `while` strictly require an expression that evaluates to a `bool`. Unlike languages such as C, JavaScript, or Python, Rust does not have "truthy" or "falsy" values. An integer, pointer, or collection cannot be implicitly evaluated as a boolean condition; it must be explicitly compared to yield a `bool`.

```rust theme={"dark"}
let count = 1;

// if count { ... } // COMPILER ERROR: expected `bool`, found integer

if count != 0 {
    // Valid control flow
}
```

## Memory Layout and Representation

In memory, a `bool` occupies exactly 1 byte (8 bits) and has an alignment of 1 byte. Rust guarantees its internal byte representation: `false` is represented as `0x00` and `true` is represented as `0x01`. This strict representation ensures safe interoperability with C-compatible Foreign Function Interfaces (FFI).

```rust theme={"dark"}
assert_eq!(std::mem::size_of::<bool>(), 1);
assert_eq!(std::mem::align_of::<bool>(), 1);
```

## Syntax and Initialization

A `bool` can be initialized using type inference or explicit type annotation.

```rust theme={"dark"}
let implicit_bool = true;
let explicit_bool: bool = false;
```

## Type Casting

Rust allows safe, unidirectional casting from a `bool` to *integer* types (e.g., `u8`, `i32`, `usize`) using the `as` keyword. `true` casts to `1` and `false` casts to `0`. Casting a `bool` directly to floating-point types (e.g., `true as f32`) is invalid and results in a compiler error (`E0605`).

```rust theme={"dark"}
let true_as_int = true as u8;    // 1
let false_as_int = false as i32; // 0

// let true_as_float = true as f32; // COMPILER ERROR E0605
```

Conversely, Rust strictly prohibits casting integer types directly back to a `bool`. You must explicitly evaluate a condition to convert an integer to a boolean.

```rust theme={"dark"}
// let invalid_bool = 1 as bool; // COMPILER ERROR
let valid_bool = 1 != 0;         // Evaluates to true
```

## Operators

The `bool` type supports three distinct categories of operators:

**Short-circuiting Logical Operators:**
These operators evaluate the right-hand operand lazily, bypassing evaluation if the final result is already determined by the left-hand operand.

* `&&` (Logical AND): Evaluates the right-hand side only if the left-hand side is `true`.
* `||` (Logical OR): Evaluates the right-hand side only if the left-hand side is `false`.

**Unary Operator:**

* `!` (Logical NOT): Inverts the boolean state immediately.

**Eager Bitwise Operators:**
When applied to `bool`, bitwise operators evaluate both operands unconditionally.

* `&` (Bitwise AND)
* `|` (Bitwise OR)
* `^` (Bitwise XOR)

```rust theme={"dark"}
let t = true;
let f = false;

let short_and = f && t; // false (short-circuits, `t` is not evaluated)
let short_or  = t || f; // true (short-circuits, `f` is not evaluated)
let eager_xor = t ^ f;  // true (eager evaluation of both operands)
let negation  = !t;     // false
```

## Trait Implementations

The `bool` primitive implements several core standard library traits that dictate its behavior:

* **`Copy` and `Clone`**: Booleans are copied by value rather than moved.
* **`Default`**: The default value of a `bool` is `false`.
* **`PartialEq` and `Eq`**: Supports strict equality comparisons.
* **`PartialOrd` and `Ord`**: Booleans are totally ordered. `false` is mathematically less than `true` (`false < true` evaluates to `true`).
* **`Hash`**: Can be safely used as keys in a `HashMap` or `HashSet`.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Rust Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
