From the course: First Look: Rust

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Method syntax

Method syntax - Rust Tutorial

From the course: First Look: Rust

Start my 1-month free trial

Method syntax

- [Instructor] Methods are similar to functions. They're declared with the function keyword and their name, they can have parameters and returner value and they can contain some code, that is run when they call from somewhere else. However, methods are different from functions, in that they're defined within the context of a struct. (mumbles) a rotate object, and the first parameter is always self, which represents the instance of the structs the method is being called on. Let's some starting code for you guys to work on. So let me just open that. You should find that in the resources section. File, open folder. (mouse clicks) Go to exercise files, resources, and open up this directory, methods. Yeah, we get these popups. Just don't worry about it and just close it. Here, let's open the main dot irus, into associate directory. As you can see here, the rectangle as a struct is declared and then an instance of the rectangle is declared here. Let's add the area function that has a…

Contents