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
}
}