From the course: Rust Essential Training

Unlock the full course today

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

Associated functions

Associated functions - Rust Tutorial

From the course: Rust Essential Training

Start my 1-month free trial

Associated functions

- [Instructor] In addition to using implementation blocks to define methods, we can also use them to define functions that are associated with a struct data type. They look similar to methods, but the key difference between a method and an associated function is that the function does not have a self input parameter. So you cannot use an associated function to reference the data from within a specific instance of a struct. Instead, associated functions can provide sub-routines that are related to the struct data type in general. You'll commonly see associated functions used as a constructor to build a new instance of a struct, perhaps with some default values. So let's define an associated function for our shuttle struct to build a new shuttle. We'll name the new function new and give it one input parameter for the name, which we'll pass in as a string slice. And the return value from this function will be a new…

Contents