From 841359f5df5c75d4f0823994bbf00bcd6ed504ab Mon Sep 17 00:00:00 2001 From: Martin Geisler Date: Wed, 3 May 2023 12:01:21 +0200 Subject: [PATCH] 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). --- src/basic-syntax/methods.md | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/basic-syntax/methods.md b/src/basic-syntax/methods.md index d11d2946..5294d88d 100644 --- a/src/basic-syntax/methods.md +++ b/src/basic-syntax/methods.md @@ -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. + +
+ +- 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. + +