1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-03-22 14:59:37 +02:00

Simplify methods slide (#594)

This tightens the language slightly. I also added a speaker note with
suggestions for more methods to add (if there is time).
This commit is contained in:
Martin Geisler 2023-05-03 12:01:21 +02:00 committed by GitHub
parent 789abc1028
commit 841359f5df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,7 +1,7 @@
# Methods
Rust has methods, they are simply functions that are associated with a particular type. The
first argument of a method is an instance of the type it is associated with:
Methods are functions associated with a type. The `self` argument of a method is
an instance of the type it is associated with:
```rust,editable
struct Rectangle {
@ -28,3 +28,18 @@ fn main() {
```
* We will look much more at methods in today's exercise and in tomorrow's class.
<details>
- Add a `Rectangle::new` constructor and call this from `main`:
```rust,editable,compile_fail
fn new(width: u32, height: u32) -> Rectangle {
Rectangle { width, height }
}
```
- Add a `Rectangle::new_square(width: u32)` constructor to illustrate that
constructors can take arbitrary parameters.
</details>