=> 함수인데 구조체 안에 선언하는 함수이고, 호출 시에는 객체.메소드명() 으로 호출 가능. 이때 파라미터는 넘길 필요 없다. self이기 때문.
메소드의 self
&self = self: &Self
자기 자신을 가리킴.
// 메소드 정의하기
struct Rectangle {
width: u32,
height: u32,
}
impl Rectangle { // &self = self: &Self
fn area(&self) -> u32 { // self는 Rectangle 자기 자신을 가리킴.
self.width * self.height
}
}
fn main() {
let rect1 = Rectangle {
width: 30,
height: 50,
};
println!(
"The area of the rectangle is {} square pixels.", rect1.area()
);
}
