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

A function in Rust is a discrete, named block of code that executes a specific sequence of statements and expressions. Defined using the `fn` keyword, functions enforce strict, static type checking for both input parameters and return values, acting as the primary boundary for Rust's ownership, borrowing, and lifetime rules.

```rust theme={"dark"}
// Visibility | Keyword | Identifier | Generics/Lifetimes | Parameters | Return Type
pub fn process_data<'a, T>(value: T, reference: &'a str) -> bool 
where 
    T: std::fmt::Display 
{
    // Statement
    let formatted = format!("{}: {}", reference, value);
    
    // Tail Expression (Implicit Return)
    formatted.len() > 10
}
```

## Parameters and Type Signatures

Function signatures in Rust require explicit type annotations for every parameter. The Rust compiler does not perform type inference on function signatures. Parameters are evaluated as patterns, allowing destructuring of tuples or structs directly in the signature.

When arguments are passed to a function, Rust's standard ownership semantics apply:

* **Pass-by-value:** Moves the value into the function's scope (or copies it, if the type implements the `Copy` trait).
* **Pass-by-reference:** Borrows the value immutably (`&T`) or mutably (`&mut T`), subject to the borrow checker's aliasing and mutability rules.

## Generics, Lifetimes, and `impl Trait`

Functions can be parameterized over types and lifetimes, declared within angle brackets `<...>` immediately following the function identifier.

* **Generic Type Parameters (`<T>`):** Allow a single function definition to operate on multiple types. The allowed types are typically restricted using trait bounds (e.g., `T: Clone`) inline or via a `where` clause.
* **Lifetime Parameters (`<'a>`):** Explicitly define the relationship between the scopes of reference parameters and return values. This ensures the borrow checker can validate memory safety across the function boundary without inspecting the function body.

```rust theme={"dark"}
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
    if x.len() > y.len() { x } else { y }
}
```

* **`impl Trait` Syntax:** Rust provides `impl Trait` as syntactic sugar for working with traits in signatures.
  * **In parameter position:** `fn print_item(item: impl Display)` acts as an anonymous generic parameter.
  * **In return position:** `fn create_iterator() -> impl Iterator<Item = i32>` specifies an opaque return type. It returns a concrete type that implements the trait, but the exact type is hidden from the caller, which is particularly useful for returning complex closures or iterator chains.

## Return Types and Expressions

Return types are declared using the `->` arrow syntax. If a function does not explicitly declare a return type, it implicitly returns the unit type `()`, which is an empty tuple representing the absence of a meaningful value.

Rust is an expression-based language. The body of a function is a block expression `{ ... }`. The value of this block evaluates to the value of its final expression.

* **Implicit Return (Tail Expression):** If the final expression of a function block does not end with a semicolon, its evaluated value is returned to the caller.
* **Explicit Return:** The `return` keyword is valid but conventionally reserved for early exits from a function. Appending a semicolon to a tail expression converts it into a statement, suppressing the value and causing the block to evaluate to `()` instead.

```rust theme={"dark"}
fn explicit_early_return(x: i32) -> i32 {
    if x < 0 {
        return 0; // Explicit early return
    }
    x * 2 // Tail expression (no semicolon)
}
```

## Function Variants

**Associated Functions and Methods**
All functions defined within an `impl` (implementation) block are associated functions, meaning they are tied to a specific type.

* **Methods:** A specific subset of associated functions that take a receiver as their first parameter (`self`, `&self`, or `&mut self`), allowing them to operate on instances of the type.
* **Non-method associated functions:** Associated functions that do not take a `self` parameter. They are invoked using the namespace path of the type (e.g., `String::new()`).

**Const Functions (`const fn`)**
Functions prefixed with `const` can be evaluated at compile time. They are highly restricted in their capabilities; they cannot perform heap allocations, use non-deterministic operations, or call non-const functions.

```rust theme={"dark"}
const fn compute_buffer_size(multiplier: usize) -> usize {
    1024 * multiplier
}
```

**Async Functions (`async fn`)**
Prefixing a function with `async` alters its return type mechanics. Instead of returning the declared type directly, the compiler desugars the function into a state machine that returns an anonymous type implementing the `Future` trait. The actual execution is deferred until the future is polled by an async runtime.

**Diverging Functions**
Functions that are guaranteed to never return to the caller are called diverging functions. They use the "never type", denoted by `!`. This is typically used for functions that contain infinite loops or terminate the process.

```rust theme={"dark"}
fn terminate_process(code: i32) -> ! {
    std::process::exit(code);
}
```

## Function Item Types and Function Pointers

Functions themselves are values and can be passed to other functions. In Rust, each function has a unique, zero-sized type known as a *function item type* (e.g., `fn() {my_func}`). These function item types automatically coerce into *function pointers*, denoted by the lowercase `fn` primitive type (e.g., `fn(i32) -> i32`), which have the size of a standard pointer.

This is distinct from the `Fn`, `FnMut`, and `FnOnce` traits, which are implemented by closures that capture their environment. Function pointers do not capture environment state and implement all three closure traits by default.

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