Code Example Template
impl Profile {
    fn activate(&mut self) {
        self.is_active = true;
    }
}

Deep-Dive System Documentation

Impl Blocks: Encapsulated Methods

To attach behaviors and methods directly to custom structures or enums, Rust uses impl (implementation) blocks. This models object-oriented patterns without using complex inheritance rules.

Self Parameters

Methods take some form of self as their first parameter:

  1. &self: Borrows the struct read-only.
  2. &mut self: Borrows the struct exclusively to modify fields.
  3. self: Takes complete ownership of the struct, consumed upon completion.
struct Profile {
    id: u32,
    is_active: bool,
}

impl Profile {
    // Associated Function (like static constructor methods)
    fn new(id: u32) -> Self {
        Profile { id, is_active: false }
    }

    // Method (mutating self)
    fn activate(&mut self) {
        self.is_active = true;
    }
}

let mut pr = Profile::new(10); // invokes associated function
pr.activate(); // invokes method

Quick Reference Guide

impl Circle { fn area(&self) -> f64 { 3.14 } }