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

Methods in Rust are functions coupled to a specific type—typically a `struct`, `enum`, or `trait` object. Unlike standalone functions, methods are defined within an `impl` (implementation) block and require a variant of `self` as their explicit first parameter to represent the instance on which the method is invoked.

## Syntax and Declaration

Methods are declared inside an `impl` block that corresponds to the type's name. The first parameter dictates the ownership and borrowing semantics of the instance.

```rust theme={"dark"}
struct CustomType {
    field: i32,
}

impl CustomType {
    // Method declaration
    fn method_name(&self, arg: i32) -> i32 {
        self.field + arg
    }
}
```

## The `self` Parameter Semantics

The first parameter of a method explicitly defines how the method interacts with the instance's memory. It is syntactic sugar for `self: Self`, `self: &Self`, or `self: &mut Self`, where the `Self` keyword acts as an alias for the implementing type.

* **`&self` (Immutable Borrow):** The method borrows the instance immutably. It can read the instance's data but cannot modify it. This is the most common method receiver.
* **`&mut self` (Mutable Borrow):** The method borrows the instance mutably. It can modify the instance's fields. The caller must have mutable access to the instance, which can be achieved via a mutable binding (`let mut`), a mutable reference (`&mut T`), or a mutably dereferenceable smart pointer.
* **`self` (Ownership Transfer):** The method takes ownership of the instance. The instance is consumed, moved into the method, and dropped at the end of the method's scope unless explicitly returned.
* **Arbitrary `self` Types:** Rust permits wrapping `self` in smart pointers, such as `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<&mut Self>`, altering the memory location or reference counting semantics of the receiver.

```rust theme={"dark"}
impl CustomType {
    fn read_only(&self) {}
    fn modify(&mut self) {}
    fn consume(self) {}
    fn boxed(self: Box<Self>) {}
}
```

## Automatic Referencing and Dereferencing

Method invocation utilizes the dot operator (`.`). During method resolution, the Rust compiler performs automatic referencing and dereferencing.

When `instance.method_name()` is called, the compiler automatically adds `&`, `&mut`, or `*` to `instance` to match the method's signature. This means you do not need to explicitly reference or dereference the calling object.

```rust theme={"dark"}
let mut instance = CustomType { field: 10 };

// The compiler automatically passes `&mut instance`
instance.modify(); 

// Syntactically equivalent to calling the method via its type path:
CustomType::modify(&mut instance);
```

## Associated Functions

Functions defined within an `impl` block that do *not* take a `self` parameter are called associated functions (analogous to static methods in other languages). Because they do not operate on an instance, they are invoked using the double colon `::` path separator rather than the dot operator.

```rust theme={"dark"}
impl CustomType {
    // Associated function (no `self`)
    fn new(value: i32) -> Self {
        Self { field: value }
    }
}

// Invocation
let instance = CustomType::new(42);
```

## Multiple `impl` Blocks

Rust allows a single type to have multiple `impl` blocks. The compiler merges all inherent methods defined across these blocks. This is structurally useful for separating distinct logical components or isolating trait implementations from inherent methods.

```rust theme={"dark"}
impl CustomType {
    fn method_one(&self) {}
}

impl CustomType {
    fn method_two(&self) {}
}
```

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