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(); }