> ## 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 If

In Rust, `if` is a control flow construct that evaluates a boolean condition to determine execution paths. Crucially, `if` in Rust is an *expression*, not a statement. This means it evaluates to a value and can be bound to variables, passed to functions, or returned from blocks.

## Syntax and Structural Rules

The syntax requires the `if` keyword followed by a condition and a block of code.

```rust theme={"dark"}
if condition {
    // execution block
} else if secondary_condition {
    // execution block
} else {
    // execution block
}
```

Rust enforces strict structural rules for `if` expressions:

1. **No Parentheses:** Parentheses `()` around the condition are syntactically valid but idiomatic Rust omits them. The compiler will issue a warning if they are included unnecessarily.
2. **Mandatory Braces:** Curly braces `{}` are strictly required around the execution block, even if the block contains only a single expression.
3. **Strict Boolean Typing:** The condition must evaluate to a strict `bool` type. Rust does not perform implicit type coercion (truthiness). Passing an integer, string, or pointer directly as a condition results in a compile-time type error.

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

// Compile error: expected `bool`, found integer
// if val { ... } 

// Valid: explicit boolean expression
if val == 1 { 
    // ...
}
```

## Expression Semantics and Type Matching

Because `if` is an expression, it yields the value of the final expression evaluated within the executed block. When an `if` expression is used to assign a value, the compiler enforces strict type uniformity across all branches.

```rust theme={"dark"}
let condition = true;

// The `if` expression evaluates to an `i32`
let result: i32 = if condition {
    5 // No semicolon, making this the return expression of the block
} else {
    10
};
```

If the branches return disparate types, the compiler will throw a `mismatched types` error.

```rust theme={"dark"}
let condition = true;

// Compile error: `if` and `else` have incompatible types
let result = if condition {
    5    // type i32
} else {
    "10" // type &str
};
```

## The Unit Type `()` in `if` Expressions

If an `if` expression does not include an `else` branch, it implicitly evaluates to the unit type `()`. Consequently, if an `if` block without an `else` evaluates to a type other than `()`, it will trigger a compile-time error.

```rust theme={"dark"}
let condition = true;

// Valid: The block evaluates to (), matching the implicit `else`
if condition {
    println!("Condition met"); // Statement ends in semicolon, yields ()
}

// Compile error: `if` block yields `i32`, but implicit `else` yields `()`
// if condition {
//     5
// }
```

## `if let` Pattern Matching

Rust provides a specialized variant called `if let`, which combines `if` with pattern matching. It is syntactic sugar for a `match` expression that only cares about a single pattern, ignoring all others.

```rust theme={"dark"}
let target_value = Some(10);

// Evaluates the block only if `target_value` matches the `Some(x)` pattern
if let Some(x) = target_value {
    // `x` is bound to the inner value (10) within this lexical scope
} else {
    // Executes if the pattern does not match (e.g., if it was `None`)
}
```

The `if let` construct adheres to the same expression semantics and type-matching rules as a standard `if` expression.

<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>
