> ## 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 Question Mark Operator

The `?` (question mark) operator is a unary postfix control-flow operator used for implicit error propagation and early returns. It evaluates an expression, unwraps and yields its inner value upon success, or immediately short-circuits the enclosing function by returning the failure state.

It serves as syntactic sugar for a `match` expression combined with an early `return`.

## Syntax

```rust theme={"dark"}
expression?
```

## Mechanics on `Result<T, E>`

When applied to a `Result<T, E>`, the `?` operator inspects the variant:

1. If the variant is `Ok(T)`, it extracts `T` and assigns it to the binding or passes it to the next expression.
2. If the variant is `Err(E)`, it immediately returns from the current function, yielding the error.

Crucially, when returning an `Err`, the `?` operator implicitly invokes `std::convert::From::from`. This automatically converts the error type of the evaluated expression into the error type specified in the enclosing function's return signature, provided the appropriate `From` trait is implemented.

**Desugared Equivalent:**

```rust theme={"dark"}
// Using the ? operator
let val = expr?;

// Conceptual desugaring
let val = match expr {
    Ok(v) => v,
    Err(e) => return Err(std::convert::From::from(e)),
};
```

## Mechanics on `Option<T>`

When applied to an `Option<T>`, the operator behaves similarly but without the type conversion step:

1. If the variant is `Some(T)`, it extracts and yields `T`.
2. If the variant is `None`, it immediately returns `None` from the enclosing function.

**Desugared Equivalent:**

```rust theme={"dark"}
// Using the ? operator
let val = expr?;

// Conceptual desugaring
let val = match expr {
    Some(v) => v,
    None => return None,
};
```

## The `std::ops::Try` Trait

Under the hood, the `?` operator is not hardcoded to `Result` and `Option`. It operates on any type that implements the `std::ops::Try` and `std::ops::FromResidual` traits.

*Note: Custom implementation of the `std::ops::Try` and `std::ops::FromResidual` traits is currently unstable. Attempting to implement these traits for user-defined types on stable Rust will result in compiler errors; it requires a nightly compiler and the `try_trait_v2` feature.*

The `Try` trait defines two associated types:

* **Output:** The type yielded upon success (e.g., `T`).
* **Residual:** The type yielded upon short-circuiting. For standard library types, these are `Result<std::convert::Infallible, E>` and `Option<std::convert::Infallible>`, representing the residual state without the success value.

When `?` is invoked, it calls `Try::branch`. If it returns `ControlFlow::Continue`, execution proceeds with the unwrapped value. If it returns `ControlFlow::Break`, it passes the residual to `FromResidual::from_residual` to construct the enclosing function's return value, and triggers the early return.

## Function Signature Constraints

Because the `?` operator forces an early return, the return type of the enclosing function must be compatible with the residual type of the expression being evaluated.

* You cannot use `?` on a `Result` inside a function that returns `()` or `Option`.
* You cannot use `?` on an `Option` inside a function that returns `Result`.

The enclosing function must return a type implementing `FromResidual` for the specific residual produced by the 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>
