From the course: Rust Essential Training

Unlock the full course today

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

Struct methods

Struct methods - Rust Tutorial

From the course: Rust Essential Training

Start my 1-month free trial

Struct methods

- [Teacher] In addition to just storing data Rust also allows us to define subroutines called methods for our structs which are similar to functions that can manipulate and use the data stored within a struct. Like functions, methods contain code that runs when you call them, they can have input parameters, as well as an output return value, and methods and functions are even both declared in Rust using the same fn keyword. The key difference between them is that methods are defined within the context of a struct and their first input parameter is always a reference to the struct itself which gives us access to the data in the specific struct instance the method is being called on. To define methods within the context of the shuttle struct we'll put them within an implementation block, which is defined using the keyword impl followed by the name of the struct type. Now, within the implementation block let's define a…

Contents