From the course: Rust Essential Training
Unlock the full course today
Join today to access over 24,400 courses taught by industry experts.
Struct methods - Rust Tutorial
From the course: Rust Essential Training
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.