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
Output:
Understanding the Code
struct Studentdefines a custom data type.impl Studentstarts an implementation block forStudent.fn display()defines a method.&selfgives the method access to the current instance.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
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:
B. &mut self
&mut self means the method borrows the current instance mutably.
It allows the method to modify the struct fields.
Example:
Output:
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:
Output:
Difference Between self, &self and &mut self
Syntax | Meaning | Purpose |
|---|---|---|
| Takes ownership | Consumes the instance |
| Immutable borrow | Reads the instance |
| Mutable borrow | Modifies the instance |
4. Methods with Parameters
Methods can accept additional parameters just like normal functions.
Example:
Output:
Here:
&selfaccesses the current Calculator instance.valueis an additional parameter.self.numberaccesses the struct field.
5. Methods with Return Values
Methods can also return values.
Example:
Output:
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
Output:
Understanding Student::new()
Notice this line:
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
Output:
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:
Here:
Stringis the type.new()is an associated function.
Another example:
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:
Output:
This example demonstrates:
new()creates an instance.display()displays student information.update_marks()modifies student marks.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 |
|
|
|
Example |
|
|
|
11. Practical Example: Bank Account
Let's create a bank account using a struct and methods.
Example Code
Output: