Lifetimes and the self Parameter in Methods

Rust methods use implicit `&self` borrowing and automatic lifetime elision to ensure safe access to struct data without manual annotation.

In Rust methods, the self parameter is implicitly borrowed as &self unless you specify &mut self or self, and lifetimes are usually elided by the compiler to match the borrow duration of the method call. You only need to write explicit lifetime annotations when a method returns a reference that could outlive the borrow of self or when the compiler cannot infer the relationship between input and output references.

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

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

    // Implicitly: fn can_hold(&self, other: &Rectangle) -> bool
    fn can_hold(&self, other: &Rectangle) -> bool {
        self.width > other.width && self.height > other.height
    }
}