Skip to main content

2 docs tagged with "impl"

View all tags

Defining Methods on Structs with `impl`

We've learned how to define structs to store data. Now, let's learn how to add behavior to our structs by defining methods. Methods are functions that are associated with a specific struct and can operate on its data.

Project: Rectangle Area Calculator (Part 2)

In Part 1 of this project, we wrote a program that calculates the area of a rectangle using a standalone function. This works, but we can make the code clearer and more organized. The area function is directly related to the Rectangle struct, so it should be a method of the struct.