#[derive(Debug)] struct Rectangle { width: u32, height: u32, } impl Rectangle { // First parameter for methods must be named 'self', but the // implementation provides some aliases like: // // self == self: Self // &self == self: &Self // // Note though that 'Self' is just an alias for the type referenced // by the impl{} block. So, here: // // impl Rectangle {} // ^ Self is an alias to Rectangle fn can_hold(&self, r: &Rectangle) -> bool { self.width > r.width && self.height > r.height } // Here we can pass just a reference to a struct // Rectangle, no need to transfer ownership fn area(self: &Rectangle) -> u32 { self.width * self.height } // Associated functions can be defined without a self parameter // so they function can be namespaced with :: fn square(size: u32) -> Self { Self { width: size, height: size, } } } fn main() { let rect = Rectangle { width: dbg!(5 * 10), height: 3, }; let sqrt = Rectangle::square(90); println!("{rect:#?} Area: {}", rect.area()); dbg!(&rect); dbg!(&sqrt); println!("{}", sqrt.can_hold(&rect)); }