Skip to content

Commit f3446a9

Browse files
cchiwqwandor
andauthored
Speaker notes for methods.md (google#202)
* Update methods.md * Update methods.md * Update methods.md * Reword explanation of methods Co-authored-by: Andrew Walbran <qwandor@google.com>
1 parent da66654 commit f3446a9

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

src/methods.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,17 @@ fn main() {
2525
}
2626
```
2727

28+
<details>
29+
30+
Key Points:
31+
* It can be helpful to introduce methods by comparing them to functions.
32+
* Methods are called on an instance of a type (such as a struct or enum), the first parameter represents the instance as `self`.
33+
* 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.
34+
* Point out the use of the keyword `self`, a method receiver.
35+
* Show that it is an abbreviated term for `self:&Self` and perhaps show how the struct name could also be used.
36+
* Explain that Self is a type alias for the type the `impl` block is in and can be used elsewhere in the block.
37+
* Note how self is used like other structs and dot notation can be used to refer to individual fields.
38+
* This might be a good time to demonstrate how the `&self` differs from `self` by modifying the code and trying to run say_hello twice.
39+
* We describe the distinction between method receivers next.
40+
41+
</details>

0 commit comments

Comments
 (0)