Associated method -> associated function

pull/134/head
Dhghomon 3 years ago committed by GitHub
parent f793ee3303
commit 062985b1b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -2999,8 +2999,8 @@ Not much green.
This is where you can start to give your structs and enums some real power. To call functions on a `struct` or an `enum`, use an `impl` block. These functions are called **methods**. There are two kinds of methods in an `impl` block.
- Regular methods: these take **self** (or **&self** or **&mut self**). Regular methods use a `.` (a period). `.clone()` is an example of a regular method.
- Associated methods (or "static" methods): these do not take self. Associated means "related to". They are written differently, using `::`. `String::from()` is an associated method, and so is `Vec::new()`. You usually see associated methods used to create new variables.
- Methods: these take **self** (or **&self** or **&mut self**). Regular methods use a `.` (a period). `.clone()` is an example of a regular method.
- Associated functions (known as "static" methods in some languages): these do not take self. Associated means "related to". They are written differently, using `::`. `String::from()` is an associated function, and so is `Vec::new()`. You see associated functions most often used to create new variables.
In our example we are going to create animals and print them.
@ -3057,7 +3057,7 @@ impl Animal {
fn main() {
let mut new_animal = Animal::new(); // Associated method to create a new animal
let mut new_animal = Animal::new(); // Associated function to create a new animal
// It is a cat, 10 years old
new_animal.check_type();
new_animal.change_to_dog();

Loading…
Cancel
Save