Methods

๋Ÿฌ์ŠคํŠธ์—์„œ ์„ ์–ธ๋œ ํƒ€์ž…์— ๋Œ€ํ•ด impl๋ธ”๋ก์— ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•˜์—ฌ ๋ฉ”์„œ๋“œ๋ฅผ ์—ฐ๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

Rust allows you to associate functions with your new types. You do this with an impl block:

#[derive(Debug)]
struct Person {
    name: String,
    age: u8,
}

impl Person {
    fn say_hello(&self) {
        println!("Hello, my name is {}", self.name);
    }
}

fn main() {
    let peter = Person {
        name: String::from("Peter"),
        age: 27,
    };
    peter.say_hello();
}