Methods
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();}
Speaker Notes
Key Points:
- Methods versus functions.
- Methods are called on an instance of a type (such as a struct or enum), the first parameter represents the instance as
self. - Developers may choose to use methods to take advantage of method receiver syntax and to help keep them more organized. By using methods we can keep all the implementation code in one predictable place.
- Methods are called on an instance of a type (such as a struct or enum), the first parameter represents the instance as
- Point out the use of the keyword
self, a method receiver.-
Explain that Self is a type alias for the type the
implblock is in and can be used elsewhere in the block. –> -
Note how self is used like other structs and dot notation can be used to refer to individual fields.
-
This might be a good time to demonstrate how the
&selfdiffers fromselfby modifying the code and trying to run say_hello twice.
-
- We describe the distinction between method receivers next.