Rust
December 2, 2026 6 min read

Methods and Associated Functions in Rust

HS
HEMANT SINGH
Technical Writer & Educator

1. What is a Method?

A method is a function associated with a particular type, such as a struct.

Methods are defined inside an impl block.

Example

struct Student {
name: String,
age: u32,
}

impl Student {
fn display(&self) {
println!("Name: {}", self.name);
println!("Age: {}", self.age);
}
}

fn main() {
let student = Student {
name: String::from("Hemant"),
age: 32,
};

student.display();
}

Output:

Name: Hemant
Age: 32

Understanding the Code

  1. struct Student defines a custom data type.
  2. impl Student starts an implementation block for Student.
  3. fn display() defines a method.
  4. &self gives the method access to the current instance.
  5. student.display() calls the method.

2. What is an impl Block?

The impl keyword is used to define methods and associated functions for a type.

Syntax

struct StructName {
// Fields
}

impl StructName {
fn method_name(&self) {
// Code
}
}

We can define multiple methods inside the same impl block.

3. Understanding self, &self and &mut self

These three forms are important when working with methods.

A. &self

&self means the method borrows the current instance immutably.

It can read the struct fields but cannot modify them through this reference.

Example:

struct Student {
name: String,
}

impl Student {
fn display(&self) {
println!("Student: {}", self.name);
}
}

fn main() {
let student = Student {
name: String::from("Hemant"),
};

student.display();
}

B. &mut self

&mut self means the method borrows the current instance mutably.

It allows the method to modify the struct fields.

Example:

struct Student {
name: String,
marks: u32,
}

impl Student {
fn update_marks(&mut self, new_marks: u32) {
self.marks = new_marks;
}
}

fn main() {
let mut student = Student {
name: String::from("Hemant"),
marks: 80,
};

student.update_marks(95);

println!("Updated Marks: {}", student.marks);
}

Output:

Updated Marks: 95

Notice that the instance must be declared with mut because we are modifying it.

C. self

A method can also take self by value.

This means the method takes ownership of the instance.

Example:

struct Student {
name: String,
}

impl Student {
fn display(self) {
println!("Student: {}", self.name);
}
}

fn main() {
let student = Student {
name: String::from("Hemant"),
};

student.display();

// student cannot be used here because ownership was moved.
}

Output:

Student: Hemant

Difference Between self, &self and &mut self


Syntax

Meaning

Purpose

self

Takes ownership

Consumes the instance

&self

Immutable borrow

Reads the instance

&mut self

Mutable borrow

Modifies the instance

4. Methods with Parameters

Methods can accept additional parameters just like normal functions.

Example:

struct Calculator {
number: i32,
}

impl Calculator {
fn add(&self, value: i32) {
let result = self.number + value;

println!("Result: {}", result);
}
}

fn main() {
let calculator = Calculator {
number: 10,
};

calculator.add(20);
}

Output:

Result: 30

Here:

  1. &self accesses the current Calculator instance.
  2. value is an additional parameter.
  3. self.number accesses the struct field.

5. Methods with Return Values

Methods can also return values.

Example:

struct Calculator {
number: i32,
}

impl Calculator {
fn square(&self) -> i32 {
self.number * self.number
}
}

fn main() {
let calculator = Calculator {
number: 5,
};

let result = calculator.square();

println!("Square: {}", result);
}

Output:

Square: 25

Here, -> i32 specifies the return type.

6. What is an Associated Function?

An associated function is a function defined inside an impl block that does not take self, &self, or &mut self as a parameter.

It is associated with the type rather than a particular instance.

A common use is creating new instances.

Example

struct Student {
name: String,
age: u32,
}

impl Student {
fn new(name: String, age: u32) -> Student {
Student {
name,
age,
}
}
}

fn main() {
let student = Student::new(
String::from("Hemant"),
32
);

println!("Name: {}", student.name);
println!("Age: {}", student.age);
}

Output:

Name: Hemant
Age: 32

Understanding Student::new()

Notice this line:

Student::new(String::from("Hemant"), 32);

We use :: instead of the dot operator.

Why?

Because new() is an associated function. It does not require an existing Student instance.

7. What is a Constructor?

A constructor is a function used to create and initialize an object or instance.

Rust does not have a special constructor keyword.

Instead, we commonly create an associated function named new().

Example

struct Employee {
name: String,
salary: u32,
department: String,
}

impl Employee {
fn new(name: String, salary: u32, department: String) -> Employee {
Employee {
name,
salary,
department,
}
}
}

fn main() {
let employee = Employee::new(
String::from("Rahul"),
25000,
String::from("IT"),
);

println!("Name: {}", employee.name);
println!("Salary: {}", employee.salary);
println!("Department: {}", employee.department);
}

Output:

Name: Rahul
Salary: 25000
Department: IT

The new() function makes instance creation cleaner and more consistent.

8. Understanding the :: Operator

The :: operator is used to access associated items such as associated functions and constants.

Example:

String::new()

Here:

  1. String is the type.
  2. new() is an associated function.

Another example:

String::from("Hello")

The from() function is also an associated function.

Remember:


Operator

Common use

.

Access fields and call methods on instances

::

Access associated functions and other associated items through a type or path

9. Multiple Methods in One impl Block

We can define many methods and associated functions inside a single impl block.

Example:

struct Student {
name: String,
marks: u32,
}

impl Student {
fn new(name: String, marks: u32) -> Student {
Student {
name,
marks,
}
}

fn display(&self) {
println!("Name: {}", self.name);
println!("Marks: {}", self.marks);
}

fn update_marks(&mut self, marks: u32) {
self.marks = marks;
}

fn is_passed(&self) -> bool {
self.marks >= 33
}
}

fn main() {
let mut student = Student::new(
String::from("Hemant"),
80
);

student.display();

student.update_marks(95);

println!("Passed: {}", student.is_passed());

student.display();
}

Output:

Name: Hemant
Marks: 80
Passed: true
Name: Hemant
Marks: 95

This example demonstrates:

  1. new() creates an instance.
  2. display() displays student information.
  3. update_marks() modifies student marks.
  4. is_passed() checks whether the student has passed.

10. Difference Between Function, Method and Associated Function


Feature

Normal Function

Method

Associated Function

Defined inside impl?

No

Yes

Yes

Uses self?

Not as a receiver

Usually yes

No

Called using

function()

instance.method()

Type::function()

Example

fn add()

student.display()

Student::new()

11. Practical Example: Bank Account

Let's create a bank account using a struct and methods.

Example Code

struct BankAccount {
account_holder: String,
balance: f64,
}

impl BankAccount {
fn new(account_holder: String, balance: f64) -> BankAccount {
BankAccount {
account_holder,
balance,
}
}

fn deposit(&mut self, amount: f64) {
self.balance += amount;
}

fn withdraw(&mut self, amount: f64) {
if amount <= self.balance {
self.balance -= amount;
} else {
println!("Insufficient Balance");
}
}

fn display_balance(&self) {
println!("Account Holder: {}", self.account_holder);
println!("Balance: {}", self.balance);
}
}

fn main() {
let mut account = BankAccount::new(
String::from("Hemant"),
10000.0
);

account.deposit(5000.0);

account.withdraw(2000.0);

account.display_balance();
}

Output:

Account Holder: Hemant
Balance: 13000