Skip to main content

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.

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.
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.
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.
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.
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.
impl CustomType {
    fn method_one(&self) {}
}

impl CustomType {
    fn method_two(&self) {}
}
Master Rust with Deep Grasping Methodology!Learn More