100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Rust

Methods and impl Blocks in Rust

Learn how to attach behavior to structs and enums using impl blocks, self receivers, and associated functions.

Functions & ClosuresBeginner8 min readJul 8, 2026
Analogies

Introduction

Methods let you attach behavior directly to a struct or enum type, similar to how classes bundle data and functions in object-oriented languages. In Rust, methods are defined inside an impl (implementation) block associated with a specific type, rather than inside the type definition itself. This keeps data definitions separate from behavior while still grouping related functionality together.

🏏

Cricket analogy: A player's profile (name, average, strike rate) is like a struct's data, while the coach adds batting drills and shot-selection routines in a separate training block, just as Rust attaches methods to a type via impl instead of inside the struct.

Syntax

rust
struct Rectangle {
    width: u32,
    height: u32,
}

impl Rectangle {
    fn area(&self) -> u32 {
        self.width * self.height
    }

    fn new(width: u32, height: u32) -> Rectangle {
        Rectangle { width, height }
    }
}

Explanation

Inside an impl block, functions that take a self parameter are called methods and are invoked with dot syntax (instance.method()). The receiver can take three forms: &self borrows the instance immutably, &mut self borrows it mutably (allowing modification), and self takes ownership of the instance, consuming it. Functions defined in an impl block without a self parameter are called associated functions; they behave like static methods and are called using the double-colon syntax on the type itself, such as Rectangle::new(...) or String::from(...).

🏏

Cricket analogy: A commentator reviewing a scorecard borrows it (&self) without altering it, but the scorer updating runs after each ball borrows mutably (&mut self); calling Rectangle::new is like the ICC issuing a fresh match ball rather than referencing an existing one.

Example

rust
struct Counter {
    value: i32,
}

impl Counter {
    fn new() -> Counter {
        Counter { value: 0 } // associated function, no self
    }

    fn increment(&mut self) {
        self.value += 1; // mutable borrow
    }

    fn value(&self) -> i32 {
        self.value // immutable borrow
    }
}

fn main() {
    let mut c = Counter::new();
    c.increment();
    c.increment();
    println!("counter = {}", c.value());
}

Output

rust
counter = 2

Key Takeaways

  • Methods are defined inside impl Blockname { ... } and use dot syntax to call.
  • &self borrows the instance immutably; &mut self borrows it mutably; self consumes it.
  • Associated functions have no self parameter and are called with Type::function().
  • A struct or enum can have multiple impl blocks with methods split across them.
  • Associated functions are commonly used as constructors, like Rectangle::new().

Practice what you learned

Was this page helpful?

Topics covered

#Rust#RustProgrammingStudyNotes#Programming#MethodsAndImplBlocksInRust#Methods#Impl#Blocks#Syntax#Functions#StudyNotes#SkillVeris